2
在我的asp.net MVC 3項目中,我想創建一個與公司相關的聯繫人。ASP.NET MVC 3 @ Html.DropDownListFor忽略selectedValue
您可以直接創建聯繫人或通過公司詳細信息視圖並添加一個新聯繫人傳遞companyId以將該公司設置爲聯繫人創建表單的下拉菜單。
問題是,我無法在下拉菜單中將傳遞的公司作爲默認值。
的Global.asax
routes.MapRoute("contactCreate", "contact/toevoegen/{companyid}", new { action = "ContactCreate", controller = "Backend", companyid = UrlParameter.Optional });
控制器方法
public ActionResult ContactCreate(int? companyid)
{
Contact contact = new Contact();
ViewBag.StatusList = srep.getContactStatusses();
ViewBag.CompanyId = companyid;
return View(contact);
}
查看
@model xxx.Models.Contact
...
<div class="editor-label">
@Html.LabelFor(model => model.bedrijf_id)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.bedrijf_id, new SelectList(ViewBag.Bedrijven, "bedrijf_id", "bedrijf_naam",ViewBag.CompanyId), "--Kies bedrijf--")
@ViewBag.CompanyId
@Html.ValidationMessageFor(model => model.bedrijf_id)
</div>
...
@ ViewBag.CompanyId公頃是一種價值。
任何想法爲什麼它沒有設置選定的值?
在我的ViewModel中構建SelectList似乎工作... – tortuga 2011-05-31 12:58:58