Javascript MVC将具有多种值类型的JS哈希表发布到MVC控制器中
我有一个js对象构建,如下所示: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();
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()
];
及