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

本文关键字:MVC 列表 绑定 JSON 数组 我可以 | 更新日期: 2023-09-27 18:27:30

是否可以将数组的JSON数组传递到我的List of Lists模型属性并正确绑定?或者我必须让我的.NET模型也成为数组的数组吗?我的客户端将JSON传递给控制器中的模型,并且模型的list>属性始终为空。

这是我的模特的样子。

[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格式的请求。谢谢

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

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
         }
      ]
   ]
}