简体   繁体   English

显示/列出来自其他模型(ViewModel)的数据?

[英]Display/list data from a different model (ViewModel)?

I have a customer model 我有一个客户模型

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

I want to display all info from all customers in a profile view. 我想在个人资料视图中显示所有客户的所有信息。 I guess it is best to use a viewmodel because I also want to show other information in profile. 我想最好使用ViewModel,因为我也想在配置文件中显示其他信息。

I created a viewmodel 我创建了一个视图模型

 public class CustomerViewModel
    {
        public string CustomerAddress1 { get; set; }
        public string CustomerAddress2 { get; set; }
        public string CustomerAddress3 { get; set; }
    }

I do not really know how my profile controller should look like and have tested 我真的不知道我的配置文件控制器应该是什么样子并经过测试

        CustomerViewModel ViewModel = new CustomerViewModel();

        return View(ViewModel);

Viewmodel is null. Viewmodel为null。

Normally, if I want to get all customers but in the customer Controller I do like this. 通常,如果我想获得所有客户,但在客户控制器中,我会这样做。

var customer = db.Customers.ToList();
return customer

And how should I list all the customers in the profile view? 我应该如何在个人资料视图中列出所有客户?

This is not working 这不行

@model xxx.ViewModels.CustomerViewModel

@foreach (var item in Model)
{
    item.Customer.CustomerAddress2
}

You want to list multiple viewmodels, so create a collection: 您要列出多个视图模型,因此创建一个集合:

var allCustomers = db.Customers.ToList();

Then map that to your viewmodel: 然后将其映射到您的视图模型:

var allCustomersProfiles = allCustomers.Select(c => new CustomerViewModel
{
    CustomerAddress1 = c.CustomerAddress1,
    CustomerAddress2 = c.CustomerAddress2,
    CustomerAddress3 = c.CustomerAddress3,  
}).ToList();

You can simplify this by directly projecting onto your viewmodel, causing the optimal SQL to be generated and only one object per row to be instantiated instead of two: 您可以通过直接投影到视图模型上来简化此过程,从而生成最佳的SQL,并且仅实例化每行一个对象,而不是两个:

var allCustomersProfiles = db.Customers.Select(c => new CustomerViewModel
{
    // ...
}.ToList()

Then tell your view to expect an enumerable model (you're going to pass List<T> but IEnumerable<T> works just as well): 然后告诉您的视图期望一个可枚举的模型(您将传递List<T>但是IEnumerable<T>可以工作):

@model IEnumerable<xxx.ViewModels.CustomerViewModel>

@foreach (var item in Model)
{
    item.Customer.CustomerAddress2
}

And return the collection to your view: 并将集合返回到您的视图:

return View(allCustomersProfiles);

I can think of 2 ways of doing this, namely: 我可以想到两种方法,即:

  • Return a list of customer view models containing only customer-related data 返回仅包含与客户相关的数据的客户视图模型的列表
  • Return a customer view model with a list of customers and any non-related customer data 返回具有客户列表和所有不相关客户数据的客户视图模型

Regarding my first option, you can return a list of customer view models to the view. 关于我的第一个选择,您可以将客户视图模型的列表返回到视图。 This will only contain customer-related data, nothing else: 这将仅包含与客户相关的数据,而没有其他内容:

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
}

In your controller's action method you will retrieve your list of customers, loop through each customer and populate a view model item for each customer and add it the customer view model list: 在控制器的操作方法中,您将检索客户列表,遍历每个客户并为每个客户填充一个视图模型项,并将其添加到客户视图模型列表中:

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

Once the list of customer view models is populated then you send this list to the view. 一旦填充了客户视图模型列表,便可以将该列表发送到视图。 The view will receive a strongly-typed list of customer view models: 该视图将收到客户视图模型的强类型列表:

@model List<YourProject.ViewModels.CustomerViewModel>

@foreach (var customerViewModel in Model)
{
     <p>@customerViewModel.Id - @customerViewModel.Name</p>
}

The second option is to return a customer view model to the view containing a list of customers and any other data that that you want to use in your view. 第二个选项是将客户视图模型返回到包含客户列表以及您要在视图中使用的任何其他数据的视图。 Let's say that you want to display store information as well as a list of customers for that store: 假设您要显示商店信息以及该商店的客户列表:

public class CustomerViewModel
{
     public CustomerViewModel()
     {
          Customers = new List<Customer>();
     }

     public List<Customer> Customers { get; set; }

     public string StoreName { get; set; }
}

In your controller action method you would populate the list of customers and store information: 在控制器操作方法中,您将填充客户列表并存储信息:

public ActionResult Profile()
{
     CustomerViewModel customerViewModel = new CustomerViewModel();
     customerViewModel.Customers = customerRepository.GetAll();
     customerViewModel.StoreName = "Test Store Name";

     return View(customerViewModel);
}

When the customer list and store information is populated then send this customer view model to the view: 当填充了客户列表和商店信息后,将该客户视图模型发送到视图:

@model YourProject.ViewModels.CustomerViewModel

<div>@Model.StoreName</div>
@foreach (var customer in Model.Cutomers)
{
     <p>@customer.Id - @customer.Name</p>
}

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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