为属性的多个值绑定模型 MVC
Model binding MVC for multiple values of properties
如果我有样本 class 说 X:
public class X
{
public string Id { get; set; }
public string Name { get; set; }
}
我从一个函数中得到 Id
和 Name
的多个值:
var Xgroups = await client.Groups.GetGroupsAsync();
和Id
和Name
可以从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 };
根据@Shyju 的建议,您可以动态创建一个包含 X 类:
的列表
var list = new List<X>();
foreach (var group in Xgroups.Value)
{
list.Add(new X
{
Id = group.Id,
Name = group.Name
}
);
}
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.
如果我有样本 class 说 X:
public class X
{
public string Id { get; set; }
public string Name { get; set; }
}
我从一个函数中得到 Id
和 Name
的多个值:
var Xgroups = await client.Groups.GetGroupsAsync();
和Id
和Name
可以从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 };
根据@Shyju 的建议,您可以动态创建一个包含 X 类:
的列表var list = new List<X>();
foreach (var group in Xgroups.Value)
{
list.Add(new X
{
Id = group.Id,
Name = group.Name
}
);
}
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.