2016-05-04 78 views
0

我有一個客戶模型顯示/列出來自不同模型(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 
} 
+1

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

回答

1

要列出多的ViewModels,所以創建一個集合:

var allCustomers = db.Customers.ToList(); 

然後映射到您的視圖模型:

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

你可以通過直接投影到您的視圖模型來簡化這一點,導致生成最優化的SQL並且只有一個o每行bject被實例化,而不是兩個:

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

我能想到的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> 
} 

我希望這有助於。

相關問題