[英]How do I perform 2 different ActionResults in the same controller within the same view?
I am trying to remove a role from my application user on ActionLink click and when it is successful send a Viewbag message. 我试图从我的应用程序用户中删除ActionLink上的角色,并在成功发送Viewbag消息时。 The issue I am having currently is that when I try to remove the user from the role, the page is being refreshed.
我目前遇到的问题是,当我尝试从角色中删除用户时,页面正在刷新。 The reason for this is because I want the admin to be able to get a list of roles he is requesting and delete from the same page, to keep the application lean.
这是因为我希望管理员能够获取他正在请求的角色列表并从同一页面删除,以保持应用程序的精益。 Here is my controller action:
这是我的控制器动作:
public ActionResult DeleteRoleForUser()
{
var list = _context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
ViewBag.Roles = list;
return RedirectToAction("GetRoles");
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult DeleteRoleForUser(string UserName, string RoleName)
{
var account = new AccountController();
ApplicationUser user = _context.Users.FirstOrDefault(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase));
if (account.UserManager.IsInRole(user.Id, RoleName))
{
account.UserManager.RemoveFromRole(user.Id, RoleName);
ViewBag.Message = "Role removed from this user successfully!";
return View("GetRoles");
}
var list = _context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
ViewBag.Roles = list;
return View("GetRoles");
}
Here is the remove link on my view 这是我视图中的删除链接
@Html.ActionLink("Remove Role", "DeleteRoleForUser", new{controller = "Role"})
I have the DeleteRoleForUser
and the GetRoles
controllers working properly/separately on different views by themselves with DeleteRoleForUser
using a listbox to display all roles in the database, however, as I mentioned earlier I would like to combine the two pieces of functionality. 我有
DeleteRoleForUser
和GetRoles
控制器自己使用DeleteRoleForUser
在不同的视图上正确/单独工作使用列表框来显示数据库中的所有角色,但是,正如我之前提到的,我想结合这两个功能。
First, your action link will cause your browser to issue a GET request, not a POST. 首先,您的操作链接将导致您的浏览器发出GET请求,而不是POST。 So your
[HttpPost]
attribute will need to change accordingly. 因此,您的
[HttpPost]
属性需要相应更改。
Second, you aren't passing in any parameters into the method. 其次,您没有将任何参数传递给方法。 Your link will need to change to
您的链接需要更改为
@Html.ActionLink("Remove Role", "DeleteRoleForUser", new{ controller = "Role", UserName="username", RoleName="role"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.