我有一個簡單的聯繫人列表應用程序。我正在使用與我的域模型完全分離的視圖模型。查看具有的IEnumerable這裏是我的指數()action方法來呈現列表:ASP.Net MVC傳遞列表<>使用IEnumerable <>查看
private AddressBookContext db = new AddressBookContext();
public ActionResult Index()
{
List<ContactListVM> viewListVM = new List<ContactListVM>();
foreach (Contact c in db.Contacts.ToList())
{
viewListVM.Add(new ContactListVM
{
ContactID = c.ContactID,
FirstName = c.FirstName,
LastName = c.LastName,
Address1 = c.Address1,
Address2 = c.Address2,
City = c.City,
State = c.State,
ZipCode = c.ZipCode,
Phone = c.Phone,
Email = c.Email,
BirthDate = c.BirthDate
});
}
return View(viewListVM);
}
有沒有辦法用更少的代碼來完成呢?