我有一個客戶模型顯示/列出來自不同模型(ViewModel)的數據?
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);
視圖模型爲空。
通常情況下,如果我想獲得所有客戶,但在客戶控制器中,我喜歡這樣做。
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});'' –