视图模型:
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();
}
Create.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在另一个视图上运行得很好。但是,这不适用于具有多个表的viewmodel。当它被发回时,LicenseHolderTypeID为0,无论我做什么。我也试过dropdownlistfor并且仍然得到0.任何助手都将不胜感激。我认为这与视图模型的传递方式有关,但我被卡住了。