mvc3 アプリケーションを作成しました。ここで、EF を使用してテーブルからドロップダウン リストを設定します。
今two dropdownlists
、私はindex.cshtml
それぞれ異なるテーブルからデータを取得しています。
コード index.cshtml:
table1
テーブルとテーブルの両方を持っているため、モデルをエンティティとして取得しましたtable2
@model Mapping.Models.mydataEntities1
しかし、問題はここにあります。両方のテーブルから値を選択できません。:(
次のコードのように、モデルを指定したときにのみ機能します@model Mapping.Models.table1
ただし、異なるテーブルから2つのドロップダウンに値を選択する必要があります
@using (Html.BeginForm())
{
@Html.DropDownListFor(
x => x.CategoryId,
new SelectList(Model.Categories, "Value", "Text"),
"-- Select category --"
)
<input type="submit" value="OK" />
}
これどうやってするの?
私はEFを使用しました
public partial class mydataEntities1 : DbContext
{
public mydataEntities1()
: base("name=mydataEntities1")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<table1> table1 { get; set; }
public DbSet<table2> table2 { get; set; }
}
}