[英]How can i fill a value from one my domain model to my edit model?
In my project i have : countries
and CountryEditModel
. 在我的项目中,我有: countries
和CountryEditModel
。
public class countries
{
public int id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
}
public class CountryEditModel
{
public int id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public bool isvalid{ get;set; }
}
countries
is my domain model which is binded with entity framework and countryEditModel
is the model which i use in my views. countries
是其绑定与实体框架我的域模型和countryEditModel
是我在我的看法使用的模型。
How can i fill values from countries
to countryEditModel
. 如何将countries
值填充到countryEditModel
。 I actually want to bind list of all countries to dropdownlist in my view and I don't want to use my countries
domain model directly in my view. 我实际上想在我的视图中将所有国家/地区的列表绑定到下拉列表,并且我不想在我的视图中直接使用我的countries
区域模型。
To solve i have done this 为了解决我已经做到这一点
var countryDomain = context.Country.Select(c => c).ToList();
var countrylist = new List<CountryEditModel>();
var countrymodel = new CountryEditModel();
foreach (var country in countryDomain)
countrymodel = new CountryEditModel()
{
Code = country.Code,
Name = country.Name,
id = country.id
};
countrylist.Add(countrymodel); countrylist.Add(countrymodel);
Is there any better way? 有什么更好的办法吗?
Answer: 回答:
Actually this is what i exactly wanted to do 其实这正是我真正想做的
var countryViewModel = context.Country.Select(c => new CountryEditModel
{
Code = c.Code,
Name = c.Name,
id = c.id
}).ToList();
As indicated by the @rohitsingh this is what he exactly wanted to do 如@rohitsingh所示,这正是他确切想要做的
var countryViewModel = context.Country.Select(c => new CountryEditModel
{
Code = c.Code,
Name = c.Name,
id = c.id
}).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.