【问题标题】:Display/list data from a different model (ViewModel)?显示/列出来自不同模型(ViewModel)的数据?
【发布时间】:2016-05-04 10:25:41
【问题描述】:

我有一个客户模型

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

Viewmodel 为空。

通常,如果我想获得所有客户,但在客户控制器中,我会这样做。

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

我应该如何在资料视图中列出所有客户?

这不行

@model xxx.ViewModels.CustomerViewModel

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

【问题讨论】:

  • var model = db.Customers.Select(x => new CustomerViewModel { CustomerAddress1 = x.CustomerAddress1, etc });

标签: c# asp.net asp.net-mvc viewmodel


【解决方案1】:

你想列出多个视图模型,所以创建一个集合:

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

【讨论】:

    【解决方案2】:

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

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

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

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

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多