APS.NET MVC Ajax: Passing a IList from the View to the Controller

Posted by Bpimenta on Stack Overflow See other posts from Stack Overflow or by Bpimenta
Published on 2010-05-17T10:20:53Z Indexed on 2010/05/17 11:20 UTC
Read the original article Hit count: 351

Filed under:
|
|

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,

© Stack Overflow or respective owner

Related posts about AJAX

Related posts about asp.net-mvc