ASP.NET MVC Ajax: Passing an IList from the View to the Controller


ASP.NET MVC Ajax: Passing an IList from the View to the Controller



I need to pass the grid rows from the view to the controller using POST. The idea is to pass an IList of objects (people) that have the following structure:

  • String Name
  • String Address
  • String ID

I want to read the data from the JQGrid and pass it to the controller to fill the IList.

I'm trying to build the data object to pass through the Ajax data parameter.

Here is the Javascript code:

$("#saveButton").click(  function()  {    var returnData = '{';    var existingIDs = $('#listPeople').getDataIDs();     if (idsPeople.length > 0)    {      for (i=0;i<idsPeople.length;i++)       {  //Trying to build the obejct data  ret = ret + '"people['+ i +'].Name":' $('#listPeople').getRowData(idsPeople[i]).Name + ',';  ret = ret + '"people['+ i +'].Address":' $('#listPeople').getRowData(idsPeople[i]).Address+ ',';  ret = ret + '"people['+ i +'].Id":' $('#listPeople').getRowData(idsPeople[i]).Id+ ',';  //If it has more than one element       if (idsPeople.length>1 && (i+1)<idsPeople.length)       {         ret = ret + ',';       }     }   }  ret = ret + '}'; 

My Ajax function for sending:

var url_all = '<%=Url.Action("SaveData") %>;  $.ajax({ type: "POST", url: url_all, data: ret, dataType: "json", success: function(){    alert("OK");  }, error: function(){    alert("Error: check SaveData");  }  }); 

My controller:

public ActionResult SaveData(IList<PeopleHeader> people){     // using debug to know if "people" variable has any values     return Json(true); } 

The problem I'm getting is an error: "System.NotSupportedException: Fixed size collection", and no data is being delivered.

I think my problem relies on creating the object... is there any simpler way of doing this procedure?

Thanks in advance,


VS 2010 MVC Formatting

1:

ASP.NET MVC Membership, Get new UserID
The problem is you're posting JSON encoded data to the action method, although action methods only accept form encoded data (aka contentType: application/www-x-form-urlencoded).. ASP.NET MVC 2 - Saving child entities on form submit I think if you simply remove the line: . what is the optimal database connection strategy dataType: "json". Working with images in asp.net MVC ViewMasterPage in design mode It should work. Should I dive into ASP.NET MVC or start with ASP.NET Webforms?Or, if you really did want to post JSON, you must try the JsonValueProvider.. ASP.NET MVC Cookie loses value when page is changed http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx. Disable cache in Silverlight HttpWebRequest


71 out of 100 based on 56 user ratings 531 reviews

^