2016-05-04 11 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; } 
} 

を持っています。私は他の情報をプロフィールにも表示したいので、ビューモデルを使うのが最善の方法だと思います。

私は本当に私のプロフィールコントローラは、のようになりますと

 CustomerViewModel ViewModel = new CustomerViewModel(); 

     return View(ViewModel); 

のViewModelがnullをテストしているのか分からないのviewmodel

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

を作成しました。

通常、私はすべての顧客を取得したいが、顧客のコントローラでは私はこれを好きです。

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

そして、どのように私は、プロファイルビューですべての顧客をリストする必要がありますか?

これは、あなたが複数のviewmodelsを一覧表示するので、コレクションを作成したい

@model xxx.ViewModels.CustomerViewModel 

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

'var model = db.Customers.Select(x =>新しいCustomerViewModel {CustomerAddress1 = x.CustomerAddress1、etc});' –

答えて

1

が動作していない:

var allCustomers = db.Customers.ToList(); 

次に、あなたのviewmodelにそれをマップ:

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>作品):行あたりbjectが2の代わりにインスタンス化する

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

この情報が役立ちます。

関連する問題