I have problem in my code. I\'m using the registration form which comes with MVC5 , I added a field \"Role\" as a Dropdownlist to assign a role to the user while creating a
The solution that worked for me in MVC5 for editing an existing user
Model (part of)
public IEnumerable Roles { get; set; }
View
@Html.DropDownListFor(model => model.UserRole, new SelectList(Model.Roles, Model.UserRole), new { @class = "form-control" })
Controller (Get)
var model = new EditUserViewModel()
{
UserName = user.UserName,
Email = user.Email,
IsEnabled = user.IsEnabled,
Id = user.Id,
PhoneNumber = user.PhoneNumber,
UserRole = userRoleName,
// a list of all roles
Roles = from r in RoleManager.Roles orderby r.Name select r.Name
};