故障模型绑定JSON数组在ASP.NET MVC 3的列表? [英] Trouble model binding a JSON array to a List in ASP.NET MVC 3?

查看:105
本文介绍了故障模型绑定JSON数组在ASP.NET MVC 3的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦模型绑定JSON数组到C#列表MVC 3。

I am having trouble model binding a JSON array to a C# list in MVC 3.

我有一个对象叫做 DockState 。它看起来是这样的:

I have an object called a DockState. It looks like this:

[Serializable]
public class DockState
{
    public bool Closed { get; set; }
    public bool Collapsed { get; set; }
    public string DockZoneID { get; set; }
    public int ExpandedHeight { get; set; }
    public Unit Height { get; set; }
    public int Index { get; set; }
    public Unit Left { get; set; }
    public bool Pinned { get; set; }
    public bool Resizable { get; set; }
    public string Tag { get; set; }
    public string Text { get; set; }
    public string Title { get; set; }
    public Unit Top { get; set; }
    public string UniqueName { get; set; }
    public Unit Width { get; set; }
}

我的行动方法接受坞状态的列表,像这样的:

My action method accepts a list of dock states, like this:

public JsonResult SaveDockStates(List<DockState> dockStates)

在客户端我建立包含所有相同的属性C# DockState 对象JSON对象的数组。我然后分配这个数组它自己的JSON对象的说法,在操作方法的财产,是这样的:

On the client I am building an array of JSON objects that contain all the same properties as the C# DockState object. I am then assigning this array to a JSON object of it's own with the property of the argument in the action method, like this:

function get_allDockStates()
{
    var allRadDocks = []; // declare array.
    var allRadControls = $telerik.radControls; // use telerik API to obtain all controls.

    // loop through all telerik controls.
    for (var i = 0; i < allRadControls.length; i++)
    {
        var element = allRadControls[i];

        // Check if control is a rad dock element.
        if (Telerik.Web.UI.RadDock && element instanceof Telerik.Web.UI.RadDock)
        {
            // Build a JSON object containing the same properties as the C# DockState
            // object. Leaving out a couple that should just be null anyway. Add new
            // JSON object to array.
            Array.add(allRadDocks,
            {
                UniqueName: element._uniqueName,
                DockZoneID: element._dockZoneID,
                Width: element._width,
                Height: element._height,
                ExpandedHeight: element._expandedHeight,
                Top: element._top,
                Left: element._left,
                Resizable: element._resizable,
                Closed: element._closed,
                Collapsed: element._collapsed,
                Pinned: element._pinned,
                Title: element._title,
                Index: element._index
            });
        }
    }
    // Return the array.
    return allRadDocks;
}

// This function is fired by the rad dock controls when they are moved.
function positionChanged(sender, e)
{
    // obtain the array of dock states.
    var dockStates = get_allDockStates();
    // Make ajax call to MVC action method to save dock states.
    jQuery.ajax({
        data: { dockStates: dockStates },
        type: 'POST',
        dataType: 'json',
        url: '/AjaxServices/DashboardService/SaveDockStates'
    });
}

在AJAX调用时不幸东西,而奇怪的情况发生。它击中的操作方法和我的列表&LT; D​​ockState方式&gt; 确实有它的项目

然而,在列表中的项目都是默认。意味着所有它们的值是默认值,而不是在请求中提交的人。

However, the items in the list are all default. Meaning all their values are the default values, not the ones submitted in the request.

我不知道为什么这个列表包含项目的正确数目,但所有项目似乎没有什么比实例化了。它们的性质并没有设定为JSON数组中的值。请求肯定包含正确的数据,如下所示:

I'm not sure why the list contains the correct number of items, but all items appear to be nothing more than instantiated. Their properties were not set to the values in the JSON array. The request definitely contains the right data, as shown here:

我是不是做错了什么?格式不正确的表格数据?是否存在与默认模型联编出了问题?

Am I doing something wrong? Is the form data formatted incorrectly? Is there a problem with the default model binder?

任何帮助很多AP preciated。

Any help is much appreciated.

我发现JSON正在被一个未使用的旧脚本覆盖。我已删除,现在JSON.stringify工作正常。这里是请求负载。

I discovered that JSON was being overridden by an unused old script. I have removed and now JSON.stringify is working fine. Here is the request payload.

好多了。但是现在,当我调试我的名单中有为零的项目。似乎没有解决这个问题,尽管我AP preciate努力:)

Much better. However now when I am debugging my list has zero items in it. Did not seem to fix the problem, though I appreciate the effort :)

推荐答案

尝试发送一个JSON请求:

Try sending a JSON request:

jQuery.ajax({
    // TODO: Never hardcode urls like this => always use URL helpers
    // when you want to generate an url in an ASP.NET MVC application
    url: '/AjaxServices/DashboardService/SaveDockStates',
    type: 'POST',
    data: JSON.stringify({ dockStates: dockStates }),
    contentType: 'application/json; charset=utf-8',
    success: function(result) {
        // TODO: process the results
    }
});

JSON.stringify 方法本身内置到现代的浏览器。如果你想支持旧版浏览器,你需要包括 json2.js 脚本到您的网页。

The JSON.stringify method is natively built into modern browsers. If you want to support legacy browsers you need to include the json2.js script to your page.

这篇关于故障模型绑定JSON数组在ASP.NET MVC 3的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆