简体   繁体   English

如何将域模型中的值填充到编辑模型中?

[英]How can i fill a value from one my domain model to my edit model?

In my project i have : countries and CountryEditModel . 在我的项目中,我有: countriesCountryEditModel

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.

相关问题 如何用我的IEnumerable填充/填充DropDown <Model> 值ASP.NET MVC - How can I fill / populate DropDown with my IEnumerable<Model> value ASP.NET MVC 当我不使用 ORM 时,如何从存储库中填充我丰富的 model? - How can I fill my rich model from repository when I am no using ORM? 如何通过使用CORS的WebAPI控制器将来自其他域的值放入模型中? - How do I put a value from a different domain in my Model through a WebAPI Controller using CORS? 我应该如何构建我的域模型 - How should I structure my domain model 如何在我的视图中访问模型中的属性 - How can I access the attributes from my model in my view 如何在编辑/更新模型期间保护我的ID实体属性? - How can I secure my ID entity property during an Edit/Update of my model? 如何在我的视图模型中从域服务访问数据? Silverlight 5应用 - How do I access the data from my domain service in my view model ? Silverlight 5 application 如何在 Razor 中单击按钮时更改模型值? - How can i change my model value on button click in Razor? 我可以在 Entity Framework Code First 中使用来自我的域模型的谓词吗? - Can I use predicates from my Domain Model in Entity Framework Code First? ASP.NET MVC 2-如何从下拉菜单中获取选定的值? - ASP.NET MVC 2 - How can I get the selected value from a drop down into my view model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM