如何将我的域模型中的值填充到我的编辑模型中?
How can i fill a value from one my domain model to my edit model?
在我的项目中,我有: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
是我的域模型,它与 entity framework 绑定,countryEditModel
是我在我的视图中使用的模型。
我如何填充从 countries
到 countryEditModel
的值。我实际上想在我的视图中将所有国家/地区的列表绑定到下拉列表,我不想在我的视图中直接使用我的 countries
域模型。
为了解决我已经这样做了
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(国家模型);
有没有更好的方法?
答案:
其实这就是我真正想做的
var countryViewModel = context.Country.Select(c => new CountryEditModel
{
Code = c.Code,
Name = c.Name,
id = c.id
}).ToList();
正如@rohitsingh 所指出的,这正是他想要做的
var countryViewModel = context.Country.Select(c => new CountryEditModel
{
Code = c.Code,
Name = c.Name,
id = c.id
}).ToList();
在我的项目中,我有: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
是我的域模型,它与 entity framework 绑定,countryEditModel
是我在我的视图中使用的模型。
我如何填充从 countries
到 countryEditModel
的值。我实际上想在我的视图中将所有国家/地区的列表绑定到下拉列表,我不想在我的视图中直接使用我的 countries
域模型。
为了解决我已经这样做了
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(国家模型);
有没有更好的方法?
答案:
其实这就是我真正想做的
var countryViewModel = context.Country.Select(c => new CountryEditModel
{
Code = c.Code,
Name = c.Name,
id = c.id
}).ToList();
正如@rohitsingh 所指出的,这正是他想要做的
var countryViewModel = context.Country.Select(c => new CountryEditModel
{
Code = c.Code,
Name = c.Name,
id = c.id
}).ToList();