将 JSON 对象数组发布到 Asp.Net MVC 2 时出现问题

发布于 2024-10-14 21:17:46 字数 1418 浏览 0 评论 0原文

我在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不咬妳我踢妳 2024-10-21 21:17:46

我不相信 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文