ASP.NET MVC Ajax:将IList从视图传递到控制器
问题描述:
我需要使用POST将视图中的网格行传递给控制器。我们的想法是通过对象具有以下结构(人)的一个IList:ASP.NET MVC Ajax:将IList从视图传递到控制器
- 字符串名称
- 字符串地址
- 字符串ID
我想读从数据JQGrid并将其传递给控制器以填充IList。
我想构建数据对象来通过Ajax数据参数。
下面是Javascript代码:
$("#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 + '}';
我的Ajax功能发送:
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");
}
});
我的控制器:
public ActionResult SaveData(IList<PeopleHeader> people){
// using debug to know if "people" variable has any values
return Json(true);
}
我得到的问题是一个错误: “System.NotSupportedException:固定大小的集合”,并且没有数据正在传递。
我认为我的问题依赖于创建对象...有没有更简单的方法来做这个过程?
由于提前,
答
问题是您发布的JSON编码数据的操作方法,但操作方法只接受形式编码数据(又名的contentType:应用程序/ WWW-X窗体-urlencoded)。
我觉得如果你只是删除行:
数据类型: “JSON”
它应该工作。或者,如果您确实想发布JSON,则可以尝试JsonValueProvider。
http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx