0

I don't understand why only the string property Search.Value is not being deserialized in my ASP.NET MVC5 Controller. Please see this:

The Json-structure sent from the Client:

{
   "draw":1,
   // ...
   "start":0,
   "length":50,
   "search":{
      "value":"This is always null in my controller",
      "regex":false
   }
}

The model I have server-side:

public class AsyncDataTableRequest
{
    public int Draw { get; set; }
    public int Start { get; set; }
    public int Length { get; set; }
    public Search Search { get; set; }
    public string Value { get; set; }
}

public class Search
{
    public string Value { get; set; }
    public bool Regex { get; set; }
}

The controller where I would like to do something with Search.Value:

public JToken AsyncLogFetching(AsyncDataTableRequest req)
{
    // req.Search.Value is null here, all other properties seem correct
    ...
}

Thank you for any help!

Edit: For a sample search with "NewYork", this the request from the tab "request header" in IE Developer tools:

GET /Log/AsyncLogFetching?draw=3&start=0&length=50&search%5Bvalue%5D=NewYork&search%5Bregex%5D=false&_=1438350434912 HTTP/1.1

The tab "request text" in IE Developer tools says "No data to display".

This is the snippet that does the GET-Request, it's copy & pasted from the jQuery DataTables Pipelining example:

settings.jqXHR = $.ajax({
    "type":     conf.method, // GET
    "url":      conf.url,
    "data":     request,
    "dataType": "json",
    "contentType": "application/json",
    "cache":    false,
    "success": function (json) {
        // ...
    }
});
peter
  • 2,103
  • 7
  • 25
  • 51
  • Can you see the content of Search being posted (for exemple in the Chrome devtools network tab)? Asking this to check if the problem is in the sending of in the receiving part. You said 'The Json-structure sent from the Client...' Did you actually see it in the request in the controller? – Michel Jul 31 '15 at 13:45
  • Hi Michel, please see my edit - Search definitly has content. – peter Jul 31 '15 at 13:52
  • 1
    Are you making a GET or a POST? I'm not sure if a GET request does the same deserializing – Michel Jul 31 '15 at 13:59
  • Most examples use POST, something like this: ` $.ajax({ url: url, type: 'POST', dataType: 'json', data: JSON.stringify(data), contentType: 'application/json; charset=utf-8' }); ` – Michel Jul 31 '15 at 13:59
  • It's a GET-request, I'll add this info to my question. – peter Jul 31 '15 at 14:02
  • Hmm, I did not know the querystring could be model-bound too to a complex object. One solution I've seen is to receive a string as parameter in stead of a complex objetc, and then to deserialze the string to an object, like here: http://stackoverflow.com/questions/13145160/binding-query-string-to-object-in-asp-net-mvc – Michel Jul 31 '15 at 14:21
  • Thanks Michel, I think you gave me the right idea, seems like it's a problem with GET-requests, see here: http://stackoverflow.com/questions/12916340/complex-type-is-getting-null-in-a-apicontroller-parameter I've got it working by using POST instead, now. – peter Jul 31 '15 at 14:23
  • 1
    Nice. Glad to be sort of of help... – Michel Jul 31 '15 at 14:41

1 Answers1

1

try to use JSON.stringify(yourObject) for send the data from client to controller

please take the reference of following How to send nested json object to mvc controller using ajax

Community
  • 1
  • 1
Amit Tiwari
  • 368
  • 1
  • 4
  • This had no effect, unfortunatly. It's still null, but thank you for your reply – peter Jul 31 '15 at 13:32
  • Amit, if I add JSON.stringifry(req) then I receive an empty model in my controller (int-properties are 0, string null). – peter Jul 31 '15 at 14:00