私はこのViewModelのすべてに頭を包み込み、正しい方法で物事を実行しようとしています。次のように、EntityFrameworkを介してSQLテーブルにリンクされたモデルがいくつかあります。
[Table("HardwareOptions", Schema = "dbo")]
public class HardwareOption {
[Key]
public int RecordID {get; set;}
[ForeignKey("Configuration")]
public string Configuration {get; set;}
public string ComputerManufacturer {get; set;}
public string ComputerModel {get; set;}
public string ComputerDescription {get; set;}
public int MonitorCount {get; set;}
public string MonitorManufacturer {get; set;}
public string MonitorModel {get; set;}
public string MonitorDescription {get; set;}
}
私はこのようなビューモデルを持っています:
public class OrderIndexViewModel {
public Customer Customer {get; set;}
public MetaUser MetaUser {get; set;}
public DbSet<HardwareOption> HardwareOptions {get; set;}
public DbSet<Machine> Machines {get; set;}
public DbSet<PendingOrder> PendingOrders {get; set;}
}
そして、このような私のコントローラーのビット:
private MyDbContext db = new MyDbContext();
OrderIndexViewModel viewmodel = new OrderIndexViewModel();
viewmodel.Customer = db.Customers.Find("myselffortesting");
viewmodel.MetaUser = db.MetaUsers.Find("myselffortesting");
viewmodel.HardwareOptions = db.HardwareOptions;
viewmodel.Machines = db.Machines;
viewmodel.PendingOrders = db.PendingOrders;
return View(viewmodel);
ご覧のとおり、私の見解では、1人の顧客/ユーザーに関する情報のみが必要ですが、HardwareOptions、Machines、およびPendingOrdersテーブル全体をクエリできる必要があります。さて、私のアーキテクチャが完全に間違っている場合は、教えてください。それがこの質問の内容です。しかし、具体的なことについては、HardwareOptionsからドロップダウンリストを作成したいとします。技術的には、それぞれSelectItem
に複数の列の値の文字列の組み合わせを言いたいのですが、今のところは、構成のように1つだけにしたいのです。それを行う正しい方法は何ですか?を操作する方法がわかりませんDbSet
。Html.DropDownList
からを作成しようとするDbSet
と、正しい数のアイテムのリストが表示されましたが、すべて「mynamespace.Models.HardwareOption」と表示されていました。それは理にかなっています、私はそれを正しく行う方法を理解することができません。