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)
{
开发者_如何学Python 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,
The problem is you're posting JSON encoded data to the action method, but action methods only accept form encoded data (aka contentType: application/www-x-form-urlencoded).
I think if you simply remove the line:
dataType: "json"
It should work. Or, if you really do want to post JSON, you can try the JsonValueProvider.
http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx
精彩评论