代码之家  ›  专栏  ›  技术社区  ›  MAK

多属性值的模型绑定MVC

  •  0
  • MAK  · 技术社区  · 8 年前

    如果我有一个样本类,说X:

    public class X
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }
    

    我得到了多个值 Id 和 Name 从函数:

    var Xgroups = await client.Groups.GetGroupsAsync();
    

    身份证件 和 姓名 Xgroups 具体如下:

    foreach (var group in Xgroups.Value)
    { 
        ids.Add(group.Id); //ids is list of id
        names.Add(group.Name); //names is list name
    }
    

    var xgroups = new X() { Ids = ids, Names = names };
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   Alfred Wallace    8 年前

    根据@Shyju的建议,您可以动态创建一个包含X个类的列表:

    var list = new List<X>();
    foreach (var group in Xgroups.Value)
    {
        list.Add(new X
            {
                Id = group.Id,
                Name = group.Name
            }
        );
    }
    
        2
  •  0
  •   Chaminda Jayanath Udawatte    8 年前
           This is converting your response from wcf service to List of class type 
            X. 
           var results =  Xgroups.Value.Select(a=>new X{ Id = a.Id,Name = a.Name 
            }).ToList();
    
        I would give a proper name to the model rather than X like  Person, 
         GroupPerson.
    
    推荐文章