繁体   English   中英

显示/列出来自其他模型(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; }
}

我想在个人资料视图中显示所有客户的所有信息。 我想最好使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM