Hello all,
I have a submit button currently being used to assign roles.
I have a second button to remove the roles. To remove the roles I call an $.ajax call and I pass my controller with the id to my action.
This works BUT my TempData[msg] isn't being stored so when the call finishes no message is displayed like when I add a role.
Also the Roles are not being refreshed with my 'return RedirectToAction()' at the end of my action method.
Here is what I have is there a way I can get those small things working or what are some more ideas to do this?
Thank you
//VIEW This is where I am getting my userId from this DropDownList
@Html.AntiForgeryToken()
<div class="row">
<div class="col-md-4">
@Html.DropDownListFor(a => a.UserId,
new SelectList(Model.lstAdmins, "UserId", "Name"),
new { @class = "form-control", @id = "userAdmins" })
@* , @onChange = "SelectedValue(this)"*@
@Html.HiddenFor(a => a.UserId)
@Html.ValidationMessageFor(a => a.UserId)
</div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
</div><div class="row">
@for(var i = 0; i < Model.lstUsers.Count(); i++)
{
<div class="col-md-4">
<div>
@Html.CheckBoxFor(u => Model.lstUsers[i].SelectedUsers)
<label>
@Html.DisplayFor(u => Model.lstUsers[i].Name)
@Html.HiddenFor(u => Model.lstUsers[i].UserId)
@Html.HiddenFor(u => Model.lstUsers[i].Name)
</label>
</div>
</div>
}
</div>
<br />
<div class="row">
<div class="form-group">
<div class="col-md-offset-0 col-md-12">
<input type="submit" value="Assign Role" class="btn btn-success" />
@* @Url.Action("RemoveAdmin", "SuperAdmin") *@
<button type="button" onclick="clickMe()"
class="btn btn-danger">
Remove Role
</button>
@Html.ActionLink("Cancel", "Dashboard", "SuperAdmin",
null, new { @class = "btn btn-danger" })
</div>
function clickMe() {
var userid = $("#userAdmins").val();
if (userid != "") {
$.ajax({
url: "RemoveAdmin/SuperAdmin",
data: { userid: userid },
type: "POST",
//success: function () {
// alert("Success");
//}
});
}
}
CONTROLLER ACTION
[HttpPost]
//[ValidateAntiForgeryToken]
public ActionResult RemoveAdmin(string userid)
{
ApplicationUser au = context.Users.Where(u => u.Id.Equals(userid,
StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
var store = new UserStore<ApplicationUser>(context);
var manager = new UserManager<ApplicationUser>(store);
manager.RemoveFromRole(au.Id, "Admin");
manager.AddToRole(au.Id, "User");
TempData["Success"] = "Roles Assigned Successfully";
return RedirectToAction("AssignAdmin");
}