ViewModel returns 视图的所有 500 多条记录的值相同
ViewModel returns same values for all 500+ records to the view
我是 ViewModels 的新手,我在这里有一个新列表,我正在通过遍历数据库 table 向其中添加项目。问题是所有返回的记录都是相同的,一遍又一遍地使用相同的记录。可能是什么问题,这是完成填充数据和传递 ViewModel 的好方法还是有更好的方法?现在 returns 大约有 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);
}
这一行需要在循环内:
DimCustomersIndexViewModel CustomerItem = new DimCustomersIndexViewModel();
原因是您想要为每个客户创建一个新的视图模型,但您当前只创建一个视图模型并更改其属性。当您将它添加到列表中时,您并不是在添加副本;而是在添加副本。您正在添加您已添加的相同视图模型。
如果 DimCustomersIndexViewModel
是一个结构,此代码将起作用,因为结构只是一包没有固有标识的值,它们被复制而不是被引用。 (Technical comparison.) 但它是一个 class(应该是),具有唯一标识,因此您要将 reference 添加到列表中的单个视图模型一遍又一遍。 Customerlist[0]
和 CustomerList[1]
以及所有其他项目指向同一个 DimCustomersIndexViewModel
对象实例,然后其属性被覆盖并保留为最后一个客户。
通过将这条线移到循环中,您将为每个客户创建一个单独的DimCustomersIndexViewModel
,每个客户都有自己的 属性集,并且 CustomerList
包含对许多不同 DimCustomersIndexViewModel
对象实例的引用。
一旦您对这个概念有了扎实的经验,以后的步骤可能是使用 AutoMapper,这样您就不必在此处维护代码中所有属性的列表。
问题是您在循环的每次迭代中添加了相同 引用对象。该对象永远不会改变(你永远不会 new
它再次起来),但你改变了对象的属性。然后你一遍又一遍地添加那个对象。您需要在循环的每次迭代中更新该对象。
我是 ViewModels 的新手,我在这里有一个新列表,我正在通过遍历数据库 table 向其中添加项目。问题是所有返回的记录都是相同的,一遍又一遍地使用相同的记录。可能是什么问题,这是完成填充数据和传递 ViewModel 的好方法还是有更好的方法?现在 returns 大约有 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);
}
这一行需要在循环内:
DimCustomersIndexViewModel CustomerItem = new DimCustomersIndexViewModel();
原因是您想要为每个客户创建一个新的视图模型,但您当前只创建一个视图模型并更改其属性。当您将它添加到列表中时,您并不是在添加副本;而是在添加副本。您正在添加您已添加的相同视图模型。
如果 DimCustomersIndexViewModel
是一个结构,此代码将起作用,因为结构只是一包没有固有标识的值,它们被复制而不是被引用。 (Technical comparison.) 但它是一个 class(应该是),具有唯一标识,因此您要将 reference 添加到列表中的单个视图模型一遍又一遍。 Customerlist[0]
和 CustomerList[1]
以及所有其他项目指向同一个 DimCustomersIndexViewModel
对象实例,然后其属性被覆盖并保留为最后一个客户。
通过将这条线移到循环中,您将为每个客户创建一个单独的DimCustomersIndexViewModel
,每个客户都有自己的 属性集,并且 CustomerList
包含对许多不同 DimCustomersIndexViewModel
对象实例的引用。
一旦您对这个概念有了扎实的经验,以后的步骤可能是使用 AutoMapper,这样您就不必在此处维护代码中所有属性的列表。
问题是您在循环的每次迭代中添加了相同 引用对象。该对象永远不会改变(你永远不会 new
它再次起来),但你改变了对象的属性。然后你一遍又一遍地添加那个对象。您需要在循环的每次迭代中更新该对象。