I'm having Enum class and i have to bind the drop down list in asp.net MVC4(using Razor view engine). I can bind the drop down list and can display in view. But i couldn't show the selected item at edit mode. Please any one help me. I'm havin following code
my ViewModelclass
public class UserViewModel { public string AccountId { get; set; } [Required] [Display(Name="First Name")] public String FirstName { get; set; } [Display(Name="Middle Name")] public String MiddleName { get; set; } [Required] [Display(Name="Last Name")] public String LastName { get; set; } public String UserRoles { get; set; } } and Enum class is,
public enum UserType // Office User Types { Officer = 1, Administrator = 2, Recruiter = 3 } and i'm using following code in my controller,
Dictionary<string, string> rolename = Enum.GetValues(typeof(UserType)) .Cast<UserType>() .Select(v => v.ToString()) .ToDictionary<string, string>(v => v.ToString()); ViewBag.Roles = rolename; return View(); and my view is,
@Html.DropDownListFor(model => model.UserRoles,new SelectList(ViewBag.Roles, "Key", "Value",Model.UserRoles.ToString()), new { id = "UserRoles" }) Please help me what is my mistake and how to display selected value in dropdown list at edit mode.
UserRolesin your model is "Officer", "Administrator", etc. and the key for the drop down is 1, 2, 3, etc.?