Display/list 来自不同模型 (ViewModel) 的数据?

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; }
}

我想在个人资料视图中显示所有客户的所有信息。我想最好使用视图模型,因为我还想在配置文件中显示其他信息。

我创建了一个视图模型

 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 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合集给您看:

return View(allCustomersProfiles);

我可以想到两种方法,即:

  • Return 仅包含客户相关数据的客户视图模型列表
  • Return 带有客户列表和任何不相关客户数据的客户视图模型

关于我的第一个选项,您可以return 将客户视图模型列表添加到视图中。这将只包含与客户相关的数据,没有别的:

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>
}

第二个选项是 return 客户视图模型到包含客户列表和您要在视图中使用的任何其他数据的视图。假设您要显示商店信息以及该商店的客户列表:

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>
}

希望对您有所帮助。