我有编辑视图:
<div class="editor-field">
@Html.DropDownList("RoleID", (IEnumerable<SelectListItem>)ViewBag.RoleID, new {class="dropdownlistCustom" })
@Html.ValidationMessageFor(model => model.RoleID)
</div>
控制器:
public ActionResult Edit(int id = 0)
{
UserDetail userDetail=db.UserDetails.Find(id);
if(userDetail!=null)
{
ViewBag.RoleID = new SelectList(db.Roles.Where(r => r.RoleStatus == "A"), "RoleID", "RoleName", userdetail.RoleID);
return View(userdetail);
}
}
模型:
[Display(Name = "Name Of the Role")]
public int RoleID { get; set; }
[ForeignKey("RoleID")]
public virtual Role Roles { get; set; }
selectedValue
尽管我从控制器设置参数,DropDownList
但不显示所选值。