3

私はこの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つだけにしたいのです。それを行う正しい方法は何ですか?を操作する方法がわかりませんDbSetHtml.DropDownListからを作成しようとするDbSetと、正しい数のアイテムのリストが表示されましたが、すべて「mynamespace.Models.HardwareOption」と表示されていました。それは理にかなっています、私はそれを正しく行う方法を理解することができません。

4

1 に答える 1

6

DbSet<T>例でaを使用して実行できる便利なことは、射影です。ドロップダウンリストに表示するプロパティを含む2番目のViewModelを定義します。

public class HardwareOptionViewModel
{
    public int Id { get; set; }
    public string Configuration { get; set; }
    public string AnotherProperty { get; set; }
    //...
}

代わりに、DbSetこのViewModelのコレクションをOrderIndexViewModel:で使用しています。

public class OrderIndexViewModel
{
    //...
    public IEnumerable<HardwareOptionViewModel> HardwareOptions { get; set; }
    //...
}

Selectそして、次の方法を使用して、データベースからこれらのプロパティのみをロードします。

viewmodel.HardwareOptions = db.HardwareOptions
    .Select(h => new HardwareOptionViewModel
    {
        Id = h.Id,
        Configuration = h.Configuration,
        AnotherProperty = h.AnotherProperty,
        //...
    })
    .ToList();

編集

次のように、コレクションをドロップダウンリストにバインドします。

@model MyNamespace.OrderIndexViewModel

...

<div>
    @Html.DropDownListFor(model => model.CurrentHardwareOptionId,
        new SelectList(Model.HardwareOptions, "Id", "Configuration",
                       Model.CurrentHardwareOptionId))
</div>

ここでは、 (例の)と同じタイプの新しいプロパティを導入しましたCurrentHardwareOptionId。これは、ページがポストバックされたときにドロップダウンリストで選択されたプロパティがバインドされることになっています。OrderIndexViewModelIdintId

public class OrderIndexViewModel
{
    //...

    public int CurrentHardwareOptionId { get; set; }
}
于 2012-09-28T20:52:28.353 に答える