ViewModels를 처음 사용하면서 새로운 목록이 있으며 데이터베이스 테이블을 반복하여 항목을 추가하고 있습니다. 문제는 다시 오는 모든 레코드가 동일한 레코드를 반복해서 사용한다는 것입니다. 무엇이 문제 일 수 있으며 데이터로 채우기를 수행하고 ViewModel을 전달하는 좋은 방법입니까 아니면 더 좋은 방법입니까? 현재 동일한 데이터를 가진 약 500 개의 레코드를 반환합니다.ViewModel은 모든 500+ 레코드에 대해 동일한 값을보기로 반환합니다.
public class DimCustomersController : Controller
{
private AdventureWorks_MBDEV_DW2008Entities db = new AdventureWorks_MBDEV_DW2008Entities();
public ActionResult CustomersIndexVM()
{
List<DimCustomersIndexViewModel> CustomerList = new List<DimCustomersIndexViewModel>();
DimCustomersIndexViewModel CustomerItem = new DimCustomersIndexViewModel();
foreach (var m in db.DimCustomers.ToList())// cold do for loop up to count
{
CustomerItem.Title = m.Title;
CustomerItem.FirstName = m.FirstName;
CustomerItem.MiddleName = m.MiddleName;
CustomerItem.LastName = m.LastName;
CustomerItem.BirthDate = m.BirthDate;
CustomerItem.MaritalStatus = m.MaritalStatus;
CustomerItem.Suffix = m.Suffix;
CustomerItem.Gender = m.Gender;
CustomerItem.EmailAddress = m.EmailAddress;
CustomerItem.AddressLine1 = m.AddressLine1;
CustomerItem.AddressLine2 = m.AddressLine2;
CustomerItem.Phone = m.Phone;
//other columns go here
CustomerList.Add(CustomerItem);
}
return View("CustomersIndexVM", CustomerList);
}
답변 내에서 루프 내부의 뷰 모델을 초기화하는 것 외에 'var CustomerList = db.DimCustomers.Select (x => 새로운 DimCustomersIndexViewModel {Title = x.Title, FirstName = x.FirstName, ......} .ToList();' –