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

时间:2016-05-04 10:25:41

标签: c# asp.net asp.net-mvc 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,因为我还想在配置文件中显示其他信息。

我创建了一个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
}

2 个答案:

答案 0 :(得分:1)

您想要列出多个视图模型,因此请创建一个集合:

var allCustomers = db.Customers.ToList();

然后将其映射到您的viewmodel:

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

您可以通过直接投影到viewmodel来简化此操作,从而生成最佳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);

答案 1 :(得分:0)

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

  • 返回仅包含客户相关数据的客户视图模型列表
  • 返回客户视图模型,其中包含客户列表和任何不相关的客户数据

关于我的第一个选项,您可以将视图中的客户视图模型列表返回。这只包含与客户相关的数据,而不是其他内容:

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

我希望这会有所帮助。