通过使用jquery.load对象的数组到MVC 3 [英] Pass an array of objects using jquery.load to MVC 3
问题描述
我所遇到试图对象的数组传递给控制器MVC3 /动作的问题。我发现在网络上多次讨论(包括在这里SO),但都没有解决我的问题。以下是我的工作:
I have run into an issue attempting to pass an array of objects to an MVC3 controller/action. I've found several discussions on the web (including here at SO) but none have solved my problem. Here is what I'm working with:
public class Person
{
public Guid TempId { get; set; }
public bool ReadOnly { get; set; }
[Required(ErrorMessage = "Required")]
public string Name { get; set; }
}
下面是我的控制器操作(目前,我已经尝试了许多变化)是这样的:
Here is what my controller action (currently, I've tried many variations) looks like:
[HttpGet]
public ActionResult AddPerson(List<Person> people)
{
if (null != people)
{
foreach (var person in people)
{
Debug.WriteLine(person );
}
}
return View();
}
我曾尝试构建我的jQuery为了呼吁采取行动的许多方面,但迄今为止唯一一个我可以去工作,如果我手动连接code数组对象为URL字符串,如下所示:
I have tried numerous ways of structuring my jquery in order to call to action, but so far the only one I can get to work is if I manually encode the array of objects into the URL string like so:
var url = "AddPerson?people%5B0%5D.TempId=9FBC6EF8-67DB-4AB4-8FCE-5DFC0F2A69F9&people%5B0%5D.ReadOnly=true&people%5B0%5D.Name=Bob+Jones&people%5B1%5D.TempId=9FBC6EF8-67DB-4AB4-8FCE-5DFC0F2A6333&people%5B1%5D.ReadOnly=false&people%5B1%5D.Name=Mary+Jones #peopleDiv";
$("#peopleDiv").load(url, updatePeopleDiv);
的人5B1%%5D.ReadOnly = FALSE
是EN codeD 人[1] .ReadOnly =假的版本
The people%5B1%5D.ReadOnly=false
is the encoded version of people[1].ReadOnly=false
我尝试过其他的东西,如:
I've tried other things such as:
var url = "AddPerson #peopleDiv";
var people = [];
people.push({'[0].TempId': '<valid guid>', '[0].ReadOnly': true, '[0].Name': 'Bob Jones' });
$("#peopleDiv").load(url, people, updatePeopleDiv); // nope
$("#peopleDiv").load(url, $.param(people, false), updatePeopleDiv); // nada
$("#peopleDiv").load(url, $.param(people, true), updatePeopleDiv); // nyet
我也试图通过包装的人[]对象内修改上述: VAR数据= {改编:人}
,然后试图发送数据
对象的各种方式。所有的方式(一个比我有手动编码工作等)导致三种结果之一:
I have also tried modifying the above by wrapping that people[] inside an object: var data = { arr: people }
and then trying to send that data
object various ways. All the ways (other than the one I got to work by manually encoding) result in one of three outcomes:
- 没有所谓的控制器操作(与它如何定义和不匹配我怎么叫吧)
- 控制器操作调用,但参数为空
- 控制器动作调用,数组中的对象元素的正确数目,但没有实际值从那些对象被转移(所以我有对象的数组都使用默认值)。
有什么建议?
推荐答案
刚刚成立的contentType为应用/ JSON
,并使用 JSON.stringify ()
。
Just set contentType to 'application/json'
and use JSON.stringify()
.
$.ajax({
contentType: 'application/json',
type: 'POST',
url: '/Controller/AddPerson',
data: JSON.stringify({
people: [
{
ReadOnly: false,
Name: 'Cristi'
},
{
ReadOnly: true,
Name: 'Matt'
}
]
}),
success: function (data) {
$("#peopleDiv").html(data);
},
error: function () {
console.trace();
}
});
此外,添加/修改/删除请求应通过POST来完成。
Also, add/edit/delete requests should be done via POST.
[HttpPost]
public ActionResult AddPerson(List<Person> people)
{
if (null != people)
{
foreach (var person in people)
{
Debug.WriteLine(person );
}
}
return PartialView("viewname");
}
这篇关于通过使用jquery.load对象的数组到MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!