2

Below is my AJAX GET request that is trying to pass few parameters including a javascript object to a mvc controller but the object is always received as null:

 var sort =  { column: 'UserName', order: 'desc' };
 var sortParameter = JSON.stringify(sort);

 $.ajax({
       url: '@Url.Action("GetUsers", "Account")',
       cache: false,
       type: 'GET',
       contentType: 'application/json; charset=utf-8',
       data: { skipRecords: vm.pageIndex * 1000, sortParam: sortParameter },
       success: function (data) {
       }
 });

The controller method looks like below:

[HttpGet]
public JsonResult GetUsers(int skipRecords, Sort sortParam, string userName = null)
{

}

Also below is the Sort class defined:

public class Sort
{
    public string column { get; set; }
    public string order { get; set; }
}

If I dont use JSON.stringify and pass just the javascript object, below is the request that gets sent:

GET /Account/GetUsers?skipRecords=0&sortParam%5Bcolumn%5D=UserName&sortParam%5Border%5D=desc&_=1408990051727 HTTP/1.1
4
  • 1
    I think you're passing in a string that happens to have JSON in it, not a Sort object. How about just sortParam: sort Commented Aug 25, 2014 at 17:52
  • @MikeChristensen Yes I tried this before doing JSON.stringify but the properties are passed as null. Commented Aug 25, 2014 at 18:02
  • Can you run Fiddler and paste in the exact HTTP request being sent? That will probably help people spot the issue. Commented Aug 25, 2014 at 18:03
  • @MikeChristensen GET /Account/GetUsers?skipRecords=0&sortParam%5Bcolumn%5D=UserName&sortParam%5Border%5D=desc&_=1408990051727 HTTP/1.1 Commented Aug 25, 2014 at 18:14

2 Answers 2

3

You're probably looking for:

 var sortParameter =  { column: 'UserName', order: 'desc' };

 $.ajax({
       url: '@Url.Action("GetUsers", "Account")',
       cache: false,
       type: 'POST',
       contentType: 'application/json; charset=utf-8',
       data: JSON.stringify({ 
         skipRecords: vm.pageIndex * 1000, 
         sortParam: sortParameter }),
       success: function (data) {
       }
 });

The reason your code doesn't work is because if you don't JSON.stringify the entire result, it is passed as a querystring encoded. You can't pass JSON as a value in a querystring encoded value.

Querystring encoded (Post or Get) looks like: a=1&b=2&c=3

So your querystring would look like skipRecords=5&sortParam={column:'UserName',order:'desc' }

MVC Won't double decode querystring and json.

Sign up to request clarification or add additional context in comments.

5 Comments

I tried this but still the object contains null properties.
Remove the stringify and just pass in the native object.
@BNL Even on doing so, properties are received as null. I have added the GET request in my question which does show the parameter values though.
You CANNOT send JSON through GET, the default model binder will not handle it (because get is ALWAYS querystring/url encoded).
POST along with JSON.Stringify works. Can you add type:'POST' in your posted code ?
0

Use POST and prepend [FromBody] to your 'Sort' object parameter in your action method.

1 Comment

I have used POST along with JSON.stringify.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.