ビューモデル:
namespace CFMembers.ViewModels
{
public class IndexData
{
public LicenseHolder LicenseHolders { get; set; }
public Installation Installations { get; set; }
}
}
コントローラ:
public ActionResult Create()
{
LicenseHolder licenseHolder = new LicenseHolder();
IndexData ViewData = new IndexData
{
LicenseHolders = licenseHolder,
};
PopulatePartyTypeDropDownList();
PopulateLicenseHolderTypeDropDownList();
return View(ViewData);
}
//
// POST: /Main/Create
[ActionName("Create"), AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateSubmit(IndexData viewModel)
{
if (ModelState.IsValid)
{
db.LicenseHolder.Add(viewModel.LicenseHolders);
db.Installation.Add(viewModel.Installations);
db.SaveChanges();
return RedirectToAction("Index");
}
return View();
}
作成.cshtml
<div class="editor-label">
@Html.LabelFor(model => model.LicenseHolders.LicenseHolderTypeID, "License Type")
</div>
<div class="editor-field">
@Html.DropDownList("LicenseHolderTypeID", String.Empty)
@Html.ValidationMessageFor(model => model.LicenseHolders.LicenseHolderTypeID)
</div>
@Html.DropDownList は別のビューで問題なく動作します。ただし、それは複数のテーブルを持つビューモデルではありませんでした。ポストバックされたとき、何をしても LicenseHolderTypeID は 0 です。dropdownlistfor も試してみましたが、まだ 0 です。アシスタントは大歓迎です。ビューモデルが返される方法に関係していると思いますが、行き詰まっています。