【发布时间】:2016-05-04 10:25:41
【问题描述】:
我有一个客户模型
public class Customer
{
public int CustomerID { get; set; }
public string CustomerName { get; set; }
public string CustomerAddress1 { get; set; }
public string CustomerAddress2 { get; set; }
public string CustomerAddress3 { get; set; }
public string CustomerAddress4 { get; set; }
public string CustomerAddress5 { get; set; }
public string CustomerAddress6 { get; set; }
public string CustomerAddress7 { get; set; }
}
我想在个人资料视图中显示所有客户的所有信息。我想最好使用视图模型,因为我还想在配置文件中显示其他信息。
我创建了一个视图模型
public class CustomerViewModel
{
public string CustomerAddress1 { get; set; }
public string CustomerAddress2 { get; set; }
public string CustomerAddress3 { get; set; }
}
我真的不知道我的配置文件控制器应该是什么样子并且已经过测试
CustomerViewModel ViewModel = new CustomerViewModel();
return View(ViewModel);
Viewmodel 为空。
通常,如果我想获得所有客户,但在客户控制器中,我会这样做。
var customer = db.Customers.ToList();
return customer
我应该如何在资料视图中列出所有客户?
这不行
@model xxx.ViewModels.CustomerViewModel
@foreach (var item in Model)
{
item.Customer.CustomerAddress2
}
【问题讨论】:
-
var model = db.Customers.Select(x => new CustomerViewModel { CustomerAddress1 = x.CustomerAddress1, etc });
标签: c# asp.net asp.net-mvc viewmodel