addusertorole

This commit is contained in:
Michael 2017-05-22 21:19:17 -04:00
parent 774b319a47
commit 1b15a34906

View file

@ -27,6 +27,42 @@ public ActionResult Index(string id = "home")
return View(); return View();
} }
public ActionResult AddUserToRole(string id)
{
var model = new AddUserToRoleViewModel();
model.Roles = new List<SelectListItem>();
var db = new ApplicationDbContext();
foreach(var r in db.Roles.ToArray())
{
var converted = r as Role;
model.Roles.Add(new SelectListItem
{
Text = converted.Name,
Value = converted.Id
});
}
model.Users = new List<SelectListItem>();
foreach(var u in db.Users.OrderBy(x => x.DisplayName).ToArray())
{
model.Users.Add(new SelectListItem
{
Text = u.DisplayName,
Value = u.Id
});
}
model.RoleId = id;
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddUserToRole(AddUserToRoleViewModel model)
{
var usermanager = HttpContext.GetOwinContext().Get<ApplicationUserManager>();
usermanager.AddToRole(model.Username, model.RoleId);
return Index("roles");
}
public ActionResult RoleDetails(string id) public ActionResult RoleDetails(string id)
{ {
var db = new ApplicationDbContext(); var db = new ApplicationDbContext();