将 JSON 对象数组发布到 Asp.Net MVC 2 时出现问题
我在将 JSON 对象数组发布到 Asp.Net MVC 中的 ActionResult 时遇到问题,其中收到的对象列表始终为空。
这是有问题的代码:
Javascript:
var lineItems = new Object();
lineItems.Entrys = new Array()
var i = 0;
var currentId = 0;
$('#pages-table td.PageId').each(function () {
currentId = $(this).html().toString().trim();
lineItems.Entrys[i] = new Object({ ID: currentId, POS: i });
i++;
});
$.ajax({
url: 'UpdatePageOrder',
data: JSON.stringify(lineItems),
contentType: 'application/json',
dataType: 'json',
traditional: true,
type: 'POST',
success: function (result) {
}
});
Asp.Net MVC
public class PageOrder
{
public string ID { get; set; }
public string POS { get; set; }
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UpdatePageOrder(List<PageOrder> list)
{
var newPageOrderList = list;
... list is always null
}
Fiddler TextView:
{"Entrys":[{"ID":"0","POS":"7"},{"ID":"1","POS":"3"}]}
编辑*
Downloaded MVC2 Futures and added to OnApplicationStarted (I'm using ninject)
ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
I am having problems posting a JSON array of objects to an ActionResult in Asp.Net MVC where the list of objects received is always null.
Here is the offending code:
Javascript:
var lineItems = new Object();
lineItems.Entrys = new Array()
var i = 0;
var currentId = 0;
$('#pages-table td.PageId').each(function () {
currentId = $(this).html().toString().trim();
lineItems.Entrys[i] = new Object({ ID: currentId, POS: i });
i++;
});
$.ajax({
url: 'UpdatePageOrder',
data: JSON.stringify(lineItems),
contentType: 'application/json',
dataType: 'json',
traditional: true,
type: 'POST',
success: function (result) {
}
});
Asp.Net MVC
public class PageOrder
{
public string ID { get; set; }
public string POS { get; set; }
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UpdatePageOrder(List<PageOrder> list)
{
var newPageOrderList = list;
... list is always null
}
Fiddler TextView:
{"Entrys":[{"ID":"0","POS":"7"},{"ID":"1","POS":"3"}]}
EDIT *
Downloaded MVC2 Futures and added to OnApplicationStarted (I'm using ninject)
ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不相信 MVC 2 对 JSON 具有自动绑定支持,这可以解释您的问题。
Phil Haack 在以下链接中讨论了一个解决方案,该解决方案将引导您构建 JsonValueProvider 来解决这个特定问题。
http ://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx
编辑:抱歉,他不带你通过实际的实现,但讨论了它并在最后提供了包含提供程序的示例的链接。 MVC 3 内置了此支持,因此如果您可以升级,您将立即解决此问题。
I don't believe MVC 2 has automatic binding support for JSON which would explain your problem.
Phil Haack discusses a solution at the following link which takes you through building a JsonValueProvider to get around this particular problem.
http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx
Edit: sorry, he doesn't take you through the actual implementation, but talks about it and provides a link to a sample containing the provider towards the end. MVC 3 has this support built-in, so if you can upgrade you would sort this problem straight away.