私は、プロファイルビュー内のすべての顧客からのすべての情報を表示したい顧客のモデル別のモデル(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
}
'var model = db.Customers.Select(x =>新しいCustomerViewModel {CustomerAddress1 = x.CustomerAddress1、etc});' –