【问题标题】:How to set an incremental property during model binding automatically如何在模型绑定期间自动设置增量属性
【发布时间】:2019-03-22 12:08:08
【问题描述】:

我的 ASP.NET Core MVC 应用程序有一个输入请求正文,我将其绑定到 C# 中的请求模型。

public class Request 
{
    public int Index {get;set;}
    public string DocType {get;set;}
    public string DocId {get;set;}
}

这是我的请求 JSON

{
"request" : [
    {
        "DocType" : "MSWORD",
        "DocId"   : "553ed6c232da426681b7c45c65131d33"
    },
    {
        "DocType" : "MSEXCEL",
        "DocId"   : "256ed6c232da426681b7c45c651317895"
    }]
}

我想将此请求映射到我的 C# 模型,以便 Index 属性自动递增。

换句话说,当我将 C# 请求反序列化为 JSON 字符串时,它应该如下所示。

{
"request" : [
    {
        "Index"   : 0,
        "DocType" : "MSWORD",
        "DocId"   : "553ed6c232da426681b7c45c65131d33"
    },
    {
        "Index"   : 1,
        "DocType" : "MSEXCEL",
        "DocId"   : "256ed6c232da426681b7c45c651317895"
    }]
}

【问题讨论】:

    标签: c# asp.net-core webhttpbinding


    【解决方案1】:

    在序列化为 JSON 之前,只需使用 LINQ 进行简单的“转换”:

    //below should be your original list instead of this test data
    var list = new List<Request>
    {
        new Request {DocId = "000", DocType = "type"},
        new Request {DocId = "111", DocType = "type"},
        new Request {DocId = "222", DocType = "type"}
    };
    
    var count = 0;
    
    var newList = list.Select(x =>
    {
        x.Index = count++;
        return x;
    }).ToList();
    

    更新 感谢 Erik 和他的评论,上面的代码可以简化为

    var newList = list.Select((x, index) =>
    {
        x.Index = index;
        return x;
    }).ToList();
    

    【讨论】:

    • Why use count when Select provides an index? newList = list.Select( (x, index) =&gt; x.Index = index....
    • @ErikPhilips 你是 100% 正确的。我忘记了。
    • @PiotrStapp 问题中的输入是JSON字符串?
    • 如果输入是 json 反序列化到列表,然后使用上述技巧添加索引
    【解决方案2】:

    声明一个静态 int 变量来保存数字并使用构造函数将值分配给索引。

    using System.Collections.Generic;
    using Newtonsoft.Json;
    
    namespace ConsoleApp2 {
      class Program {
        static void Main() {
    
          string json = @"[{'DocType' : 'MSWORD','DocId'   : '553ed6c232da426681b7c45c65131d33'},{'DocType' : 'MSEXCEL','DocId'   : '256ed6c232da426681b7c45c651317895'}]";
          Request.Seed = 1;
          var r = JsonConvert.DeserializeObject<List<Request>>(json);
          Request.Seed = 100000;
          r = JsonConvert.DeserializeObject<List<Request>>( json );
    
        }
      }
      public class Request {
        public static int Seed { get; set; }
    
        public Request() {
          Index = Seed++;
        }
        public int Index { get; set; }
        public string DocType { get; set; }
        public string DocId { get; set; }
      }
    }
    

    【讨论】:

    • 所以应用程序在其整个生命周期内只能反序列化一次?
    • 您的代码仅有效一次。在第二次调用中,索引不会从零开始,而是从“最后一个”值开始。
    • @PiotrStapp 我不认为我们应该为您编写一个完整的解决方案。
    • @ErikPhilips 感谢 cmets,这是可行的解决方案。我希望它能回答您的问题
    猜你喜欢
    • 2014-07-21
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多