Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax post to ASP.net MVC controller - object properties are null

I've got an ajax post being constructed like this:

var myData = [     {         id: "a",         name: "Name 1"     },     {         id: "b",         name: "Name 2"     } ];  $.ajax({     type: 'POST',     url: '/myurl/myAction',     data: { items: myData },     dataType: 'json',     error: function (err) {         alert("error - " + err);     } }); 

And an MVC controller:

[HttpPost] public JsonResult MyAction(MyClass[] items) {  } 

MyClass is just a simple representation of the data:

public class MyClass {     public string Name {get; set; }     public string Id {get; set; } } 

When the javascript makes the post request, the controller action does indeed receive 2 items, however the properties (id, name) in these items are null.

Checking the request in fiddler, the body looks like this:

Name                 | Value items[0][Name]       | Name 1 items[0][Id]         | a items[1][Name]       | Name 2 items[1][Id]         | b 

Have I missed something?

like image 750
Alex Avatar asked May 14 '13 15:05

Alex


People also ask

Can we use Ajax in MVC?

The MVC Framework contains built-in support for unobtrusive Ajax. You can use the helper methods to define your Ajax features without adding a code throughout all the views. This feature in MVC is based on the jQuery features. To enable the unobtrusive AJAX support in the MVC application, open the Web.

Can Ajax work with ASP NET?

Calling Web Services with ASP.NET AJAXAll of this can be done without resorting to postback operations. While the ASP.NET AJAX UpdatePanel control provides a simple way to AJAX enable any ASP.NET page, there may be times when you need to dynamically access data on the server without using an UpdatePanel.

What is AJAX call in ASP NET MVC?

It is a client-side script that communicates to and from a server/database without the need for a postback or a complete page refresh. The Ajax speeds up response time. In other words, Ajax is the method of exchanging data with a server, and updating parts of a web page, without reloading the entire page.


1 Answers

Have I missed something?

Yes, take a look at the following article to understand the correct wire format that the default model binder expects for binding collections. In other words, for this to work, instead of:

items[0][Name]       | Name 1 items[0][Id]         | a items[1][Name]       | Name 2 items[1][Id]         | b 

your payload should have looked like this:

items[0].Name       | Name 1 items[0].Id         | a items[1].Name       | Name 2 items[1].Id         | b 

Unfortunately with jQuery it can be quite frustrating to achieve this payload. For this reason I would recommend that you use a JSON payload if you want to send complex objects/arrays to your server with AJAX:

$.ajax({     type: 'POST',     url: '/myurl/myAction',     data: JSON.stringify({ items: myData }),     contentType: 'application/json',     error: function (err) {         alert("error - " + err);     } }); 

Things to notice:

  • data: JSON.stringify({ items: myData }) instead of data: { items: myData }
  • Added contentType: 'application/json'
  • Gotten rid of dataType: 'json'

Now your payload looks like this:

{"items":[{"id":"a","name":"Name 1"},{"id":"b","name":"Name 2"}]} 
like image 97
Darin Dimitrov Avatar answered Sep 19 '22 19:09

Darin Dimitrov