Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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
C# 与非顺序索引的JSON数组/对象的模型绑定_C#_Jquery_Ajax_Asp.net Mvc_Json - Fatal编程技术网

C# 与非顺序索引的JSON数组/对象的模型绑定

C# 与非顺序索引的JSON数组/对象的模型绑定,c#,jquery,ajax,asp.net-mvc,json,C#,Jquery,Ajax,Asp.net Mvc,Json,参考Phil Haack的,我可以绑定到视图模型,而无需使用顺序标记,例如: public class FooViewModel { public int Id { get; set } public decimal Amount { get; set; } public ICollection<Bar> Bars { get; set; } } public class BarViewModel { public int Id { get; s

参考Phil Haack的,我可以绑定到视图模型,而无需使用顺序标记,例如:

public class FooViewModel
{
    public int Id { get; set }

    public decimal Amount { get; set; }

    public ICollection<Bar> Bars { get; set; }
}

public class BarViewModel
{
    public int Id { get; set; }

    public string Baz { get; set; }
}
通过正常HTTP Post请求提交表单时,
正确填充。但是,如果我将表单序列化为JSON,即使用序列化表单,然后通过AJAX提交,则JSON负载不正确:

{
    "Id": 1,
    "Amount": 26.4,
    "Bars": {
         "123": {
             "Id": 1,
             "Baz": "Chips"
         },
         "caliente": {
             "Id": 2,
             "Baz": "Salsa"
         },
         "Index": "caliente" <-- Extra property in the JSON object
    }
}
{
“Id”:1,
“金额”:26.4,
“酒吧”:{
"123": {
“Id”:1,
“Baz”:“芯片”
},
“卡连特”:{
“Id”:2,
“巴兹”:“萨尔萨”
},
“索引”:“caliente”尝试使用

Dictionary<string, Bar> 
字典

对于Bars参数,它应该能够理解您正在获取的序列化JSON。

尝试
$('form').serialize();
.serializeArray()
@StephenMuecke这是不可能的,因为它不
。serialize()
返回参数字符串而
。serializeArray()
不序列化嵌套对象。
Dictionary<string, Bar>