【问题标题】:Binding an array of objects in MVC在 MVC 中绑定对象数组
【发布时间】:2013-12-19 02:10:37
【问题描述】:

我在 MVC5 中绑定键值对对象数组时遇到问题。这是 JSON:

{
    "expiration": "2013-12-03T04:30:41.206Z",
    "conditions": [
        {
            "acl": "private"
        },
        {
            "bucket": "ryvus.upload.test"
        },
        {
            "Content-Type": "application/vnd.ms-excel"
        },
        {
            "success_action_status": "200"
        },
        {
            "key": "fc42ae8a-1f6e-4955-a669-8272ad650cb9.csv"
        },
        {
            "x-amz-meta-qqfilename": "simpleupload.csv"
        }
    ]
}

如果我尝试像这样将 条件 绑定到 Dictionary<string, string>

// View Model
public class ResponseVM
{
    public DateTime Expiration { get; set; }
    public Dictionary<string, string> Conditions { get; set; }
}
// Controller Action
public ActionResult MyHandler(ResponseVM s3Response)
{
   //do something
   return JSON(null);
}

我得到了 6 个条目,其中键为“0”、“1”、“2”、“3”、“4”、“5”和空值。我看起来很接近,但我尝试了很多不同的类型都没有成功,Dictionary&lt;string, string&gt; 是我能得到的最好的。

我在这里遗漏了一些完全明显的东西吗?

【问题讨论】:

  • 可以修改您的 JSON 架构吗?
  • conditions JSON 不适合字典的预期架构,因此您需要修改 JSON 架构或自定义反序列化
  • 不,很遗憾,我无法更改 JSON 架构,但我可以更改视图模型。
  • 您能改为更改conditions 数据类型吗?它真的应该是一本字典吗?
  • 嘿,这正是我的问题,什么类型会映射到这个 JSON?我尝试了一堆不同的类型都没有成功。

标签: c# asp.net-mvc json asp.net-mvc-5


【解决方案1】:

由于无法更改 JSON 方案,最简单的方法是更改​​类以匹配 JSON。

public class ResponseVM
{
    public DateTime Expiration { get; set; }
    public List<Dictionary<string, string>> Conditions { get; set; }
}

Condition 是地图列表。

顺便说一句,视图模型 (VM) 的目的是抽象视图。不是用来输入的。

【讨论】:

    【解决方案2】:
    $.ajax({
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(data),
        url: "your url",
        contentType: 'application/json; charset=utf-8',
        success: function (t) {
            // code here
        }
    });
    

    试试看。

    我没有绑定到字典,但是数组可以正常工作。

    【讨论】:

      【解决方案3】:

      我认为问题在于conditions 结构不能映射到键值对……conditions 结构实际上是 一组不同类型的对象,例如第一个对象可以这样建模:

          public class ACL {
              public string acl { get; set; }
          }
      

      下一个对象如:

          public class Bucket {
              public string bucket { get; set; }
          }
      

      等等。它不是可绑定的键值对。

      要让条件结构绑定到KeyValuePair&lt;TKey, TValue&gt; 结构,您希望 JSON 看起来更像这样:

          {
              "expiration": "2013-12-03T04:30:41.206Z",
              "conditions": [
                  {
                      Key: "acl",
                      Value: "private"
                  },
                  {
                      Key: "bucket",
                      Value: "ryvus.upload.test"
                  },
                  ...
              ]
          }
      

      编辑:如果您无法更改 JSON 架构并且您不打算编写自定义活页夹,那么以下结构应该可以工作,.. 但它是 不漂亮,因为您在conditions 中的每个项目都有NVPair 的实例。

      您的模型:

          public class NVPair {
              public string acl { get; set; }
              public string bucket { get; set; }
              [JsonProperty("Content-Type")]
              public string ContentType { get; set; }
              public string success_action_status { get; set; }
              public string key { get; set; }
              ...
          }
      
          public class ResponseVM
          {
              public DateTime Expiration { get; set; }
              public List<NVPair> Conditions { get; set; }
          }
      

      这样每个nvp都会有一个单独的项目。但它应该绑定。

      如果您不想使用 JSON.Net 的 JsonProperty 属性,您可以使用 DataContract,例如 here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-24
        • 2020-09-29
        相关资源
        最近更新 更多