I have the DropDownList in view as below,
@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)
Tried both DropDownList
and DropDownListFor
, both are not getting the correct SelectedValue
.
From Controller, I am setting the selectedValue
param (see Last parameter of 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);
}
}
Model :
[Display(Name = "Name Of the Role")]
public int RoleID { get; set; }
[ForeignKey("RoleID")]
public virtual Role Roles { get; set; }