Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript MVC将具有多种值类型的JS哈希表发布到MVC控制器中_Javascript_Asp.net Mvc_Jquery - Fatal编程技术网

Javascript MVC将具有多种值类型的JS哈希表发布到MVC控制器中

Javascript MVC将具有多种值类型的JS哈希表发布到MVC控制器中,javascript,asp.net-mvc,jquery,Javascript,Asp.net Mvc,Jquery,我有一个js对象构建,如下所示: var entity = new Object(); entity["1"] = Property1.GetValue(); entity["2"] = Property2.GetValue(); entity["3"] = Property3.GetValue(); entity["4"] = Property4.GetValue(); entity["5"] = Property5.GetValue();

我有一个js对象构建,如下所示:

    var entity = new Object();
    entity["1"] = Property1.GetValue();
    entity["2"] = Property2.GetValue();
    entity["3"] = Property3.GetValue();
    entity["4"] = Property4.GetValue();
    entity["5"] = Property5.GetValue();
    entity["6"] = Property6.GetValue();
    entity["7"] = Property7.GetValue();
    entity["8"] = Property8.GetValue();
    entity["9"] = Property9.GetValue();
    entity["10"] = Property10.GetValue();
    entity["11"] = Property11.GetValue();
    entity["12"] = Property12.GetValue();
    entity["13"] = Property13.GetValue();
    entity["14"] = Property14.GetValue();
    entity["15"] = Property15.GetValue();
    var data = JSON.stringify(
    {
        entityID: 1,
        data: entity
    });
    $.ajax({ 
            type: "POST",
            url: "/Entity/Update",
            data: data, 
            contentType: "application/json", 
            traditional: true,
            success: function (data) { 
                alert("koko");
            },
            error:function (xhr, ajaxOptions, thrownError) { 
                alert(xhr.status); 
                alert(ajaxOptions); 
                alert(thrownError); 
            }
    }); 
我发布的内容如下:

    var entity = new Object();
    entity["1"] = Property1.GetValue();
    entity["2"] = Property2.GetValue();
    entity["3"] = Property3.GetValue();
    entity["4"] = Property4.GetValue();
    entity["5"] = Property5.GetValue();
    entity["6"] = Property6.GetValue();
    entity["7"] = Property7.GetValue();
    entity["8"] = Property8.GetValue();
    entity["9"] = Property9.GetValue();
    entity["10"] = Property10.GetValue();
    entity["11"] = Property11.GetValue();
    entity["12"] = Property12.GetValue();
    entity["13"] = Property13.GetValue();
    entity["14"] = Property14.GetValue();
    entity["15"] = Property15.GetValue();
    var data = JSON.stringify(
    {
        entityID: 1,
        data: entity
    });
    $.ajax({ 
            type: "POST",
            url: "/Entity/Update",
            data: data, 
            contentType: "application/json", 
            traditional: true,
            success: function (data) { 
                alert("koko");
            },
            error:function (xhr, ajaxOptions, thrownError) { 
                alert(xhr.status); 
                alert(ajaxOptions); 
                alert(thrownError); 
            }
    }); 
post为空,但MVC控制器为数据参数获取空参数

MVC方法:

    public void Update(int entityID, IDictionary<string, object> data)
public void更新(int-entityID,IDictionary数据)
问题是这些值可以是不同的类型,而不仅仅是字符串或整数。这就是问题所在。
有没有办法使默认模型绑定器正确读取对象,或者我必须编写自定义模型绑定器?

我没有深入探讨为什么它在MVC3中不起作用(反序列化有问题?),但我在MVC4中检查了它-一切都很好

如果您无法切换到MVC4,我的建议是重构
数据
对象。我不知道您的示例与实际问题有多接近,但您可以使用数组而不是字典-在任何情况下,您的字典键都是索引

因此,以下内容将在MVC3中起作用:

var entity = [
    Property1.GetValue(),
    Property2.GetValue(),
    Property3.GetValue(),
    Property4.GetValue()
];