我可以将JSON数组数组绑定到列表的MVC列表

时间:2014-10-22 15:05:37

标签: c# asp.net-mvc json model-view-controller asp.net-web-api2

是否可以将JSON数组数组传递给我的List of Lists模型属性并正确绑定?或者我必须使我的.NET模型也成为一个数组数组?我的客户端将JSON传递给控制器​​中的模型和列表>模型的属性总是空的。

以下是我的模型的外观。

[DataContract]
public class EntityModel
{
    [DataMember]
    public string Username { get; set; }
    [DataMember]
    public List<List<SocialMediaModel>> socialMedias { get; set; }
}

以下是“SocialMediaModel”的外观

[DataContract]
public class SocialMediaModel
{
    [DataMember]
    public string Name { get; set;}
    [DataMember]
    public string URL { get; set; }
    [DataMember]
    public string Handle { get; set; }
    [DataMember]
    public bool Can_Solicit { get; set; }
}

然后这是控制器的方法签名

public PostResponse Post(string token, EntityModel entity)
{
    //Implementation here
}

如果可以,JSON看起来怎么样?我正在尝试用他们的JSON格式化请求帮助我的客户。 谢谢!

1 个答案:

答案 0 :(得分:1)

ASP.NET MVC模型绑定可以将JSON数组映射到通用列表。以下是JSON的示例:

{
   "Username":"Username3c800c78-bf8a-4da2-8ab4-cb828df19ec0",
   "socialMedias":[
      [
         {
            "Name":"Name1d4c5229-1ca1-496d-8433-5bbfcb22641e",
            "URL":"URL1150cbf7-6fae-41a9-b899-e4679a02364d",
            "Handle":"Handled486c626-1480-4339-bbba-77c965e9a79f",
            "Can_Solicit":true
         },
         {
            "Name":"Name8af140ce-6ad1-46e1-8d6b-9dca5aa6e622",
            "URL":"URLc2bf8d29-423f-4df4-a958-200405a92558",
            "Handle":"Handle6d8ef6a9-15f7-42be-be70-54d7a9b7ae47",
            "Can_Solicit":false
         },
         {
            "Name":"Nameb1869836-63fe-4bd0-a70d-498289f0cded",
            "URL":"URLeb29ce8e-eff3-42e9-bc8d-86da0c8107b1",
            "Handle":"Handle9fe10130-35cb-4eda-a46f-881b73cac24f",
            "Can_Solicit":true
         }
      ],
      [
         {
            "Name":"Name435c040b-a235-41cd-ae8c-b12169ce1dfd",
            "URL":"URLdf9654bf-527b-42fc-9f99-2c0d031f8f6f",
            "Handle":"Handle5ee19ab0-6093-4497-9636-a11f751bd15c",
            "Can_Solicit":false
         },
         {
            "Name":"Name582ea19a-911f-49ae-8e48-2a3429155959",
            "URL":"URL7d99662e-463d-4b8e-9a4d-56bfadce374f",
            "Handle":"Handle530069e0-b1c6-4d9a-b99d-da245b36b2e8",
            "Can_Solicit":true
         },
         {
            "Name":"Name2ff4edd8-03ce-41cc-87ec-e18194ada6ed",
            "URL":"URL41614488-f52a-4f37-a0dd-bc34f1207b19",
            "Handle":"Handle17fb8b9b-0365-4cce-805e-90752358a863",
            "Can_Solicit":false
         }
      ],
      [
         {
            "Name":"Namebd1938d5-17f9-4cb7-b859-a20ab7affc77",
            "URL":"URLa66d2033-c373-497a-a211-7668bd5ad014",
            "Handle":"Handle2842ff4e-2afd-4813-b706-2c48eb9a2d06",
            "Can_Solicit":true
         },
         {
            "Name":"Namecc874248-c5ec-4a04-92d5-48121d24d833",
            "URL":"URL73040afb-8b73-48ea-9c8e-9b1be7f31fe0",
            "Handle":"Handle98d10e5b-affb-4a13-b2b7-4001165b42d5",
            "Can_Solicit":false
         },
         {
            "Name":"Name4f24b8dc-ede7-48d8-9001-7cefeaf4fc16",
            "URL":"URLfa94bb52-8d66-4e1a-9373-8b909808ecf3",
            "Handle":"Handle03e43ade-8f49-47d8-bcdd-70e8b3c01740",
            "Can_Solicit":true
         }
      ]
   ]
}