[英]Display/list data from a different model (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; }
}
我想在個人資料視圖中顯示所有客戶的所有信息。 我想最好使用ViewModel,因為我也想在配置文件中顯示其他信息。
我創建了一個視圖模型
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為null。
通常,如果我想獲得所有客戶,但在客戶控制器中,我會這樣做。
var customer = db.Customers.ToList();
return customer
我應該如何在個人資料視圖中列出所有客戶?
這不行
@model xxx.ViewModels.CustomerViewModel
@foreach (var item in Model)
{
item.Customer.CustomerAddress2
}
您要列出多個視圖模型,因此創建一個集合:
var allCustomers = db.Customers.ToList();
然后將其映射到您的視圖模型:
var allCustomersProfiles = allCustomers.Select(c => new CustomerViewModel
{
CustomerAddress1 = c.CustomerAddress1,
CustomerAddress2 = c.CustomerAddress2,
CustomerAddress3 = c.CustomerAddress3,
}).ToList();
您可以通過直接投影到視圖模型上來簡化此過程,從而生成最佳的SQL,並且僅實例化每行一個對象,而不是兩個:
var allCustomersProfiles = db.Customers.Select(c => new CustomerViewModel
{
// ...
}.ToList()
然后告訴您的視圖期望一個可枚舉的模型(您將傳遞List<T>
但是IEnumerable<T>
可以工作):
@model IEnumerable<xxx.ViewModels.CustomerViewModel>
@foreach (var item in Model)
{
item.Customer.CustomerAddress2
}
並將集合返回到您的視圖:
return View(allCustomersProfiles);
我可以想到兩種方法,即:
關於我的第一個選擇,您可以將客戶視圖模型的列表返回到視圖。 這將僅包含與客戶相關的數據,而沒有其他內容:
public class CustomerViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string AddressLine3 { get; set; }
// Add more customer-related properties if needed
}
在控制器的操作方法中,您將檢索客戶列表,遍歷每個客戶並為每個客戶填充一個視圖模型項,並將其添加到客戶視圖模型列表中:
public class CustomerController : Controller
{
private ICustomerRepository customerRepository;
public CustomerController(ICustomerRepository customerRepository)
{
this.customerRepository = customerRepository;
}
public ActionResult Profile()
{
List<CustomerViewModel> customerViewModels = new List<CustomerViewModel>();
List<Customer> customers = customerRepository.GetAll();
foreach (Customer customer in customers)
{
CustomerViewModel customerViewModel = new CustomerViewModel();
customerViewModel.Id = customer.Id;
customerViewModel.Name = customer.Name;
// Populate the rest of the properties
customerViewModels.Add(customerViewModel);
}
return View(customerViewModels);
}
}
一旦填充了客戶視圖模型列表,便可以將該列表發送到視圖。 該視圖將收到客戶視圖模型的強類型列表:
@model List<YourProject.ViewModels.CustomerViewModel>
@foreach (var customerViewModel in Model)
{
<p>@customerViewModel.Id - @customerViewModel.Name</p>
}
第二個選項是將客戶視圖模型返回到包含客戶列表以及您要在視圖中使用的任何其他數據的視圖。 假設您要顯示商店信息以及該商店的客戶列表:
public class CustomerViewModel
{
public CustomerViewModel()
{
Customers = new List<Customer>();
}
public List<Customer> Customers { get; set; }
public string StoreName { get; set; }
}
在控制器操作方法中,您將填充客戶列表並存儲信息:
public ActionResult Profile()
{
CustomerViewModel customerViewModel = new CustomerViewModel();
customerViewModel.Customers = customerRepository.GetAll();
customerViewModel.StoreName = "Test Store Name";
return View(customerViewModel);
}
當填充了客戶列表和商店信息后,將該客戶視圖模型發送到視圖:
@model YourProject.ViewModels.CustomerViewModel
<div>@Model.StoreName</div>
@foreach (var customer in Model.Cutomers)
{
<p>@customer.Id - @customer.Name</p>
}
我希望這有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.