【发布时间】:2013-03-11 12:49:00
【问题描述】:
我有如下视图中的 DropDownList,
@Html.DropDownListFor(model => model.RoleID, (IEnumerable<SelectListItem>)ViewBag.RoleID, new { @class = "dropdownlistCustom" })
@Html.DropDownList("RoleID", (IEnumerable<SelectListItem>)ViewBag.RoleID, new { @class = "dropdownlistCustom" })
@Html.ValidationMessageFor(model => model.RoleID)
尝试了DropDownList 和DropDownListFor,两者都没有得到正确的SelectedValue。
在 Controller 中,我正在设置 selectedValue 参数(请参阅 SelectList() 的最后一个参数)。
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; }
【问题讨论】:
-
在控制器中,您是从数据库中获取用户详细信息还是创建新对象?
-
我正在编辑(
[GET]请求Edit)...即。我正在获取数据。我完成了大部分调试,并且在userdetail.RoleID中得到了正确的值
标签: asp.net-mvc asp.net-mvc-4 html.dropdownlistfor list.selectedvalue