我目前正在工作,我有一个提交按钮目前正用于分配角色。我还有第二个按钮来删除角色。要删除角色,我调用$ .ajax调用,并将带有id的控制器传递给我的操作。这样可以正常工作,但不会存储我的TempData [msg],因此在通话结束时不会显示任何消息,就像我添加角色时一样。另外,在我的操作方法结尾处,未使用“ return redirectToAction()”刷新角色。 这就是我所拥有的解决这些小问题的方法,或者还有什么其他想法可以实现? 谢谢
//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> <script type="text/javascript">
//$(function () {
function clickMe() {
var userid = $("#userAdmins").val();
if (userid != "") {
$.ajax({
url: "RemoveAdmin/SuperAdmin",
data: { userid: userid },
type: "POST",
//success: function () {
// alert("Success");
//},
//error: function () {
// alert("Error");
//}
});
}
//window.location.href =
//'Url.Action("RemoveAdmin", "SuperAdmin")/' + userid;
//alert("val" + selectedValue);
}
// 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");
}