简体   繁体   English

如何在同一视图中的同一个控制器中执行2个不同的ActionResults?

[英]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. 我有DeleteRoleForUserGetRoles控制器自己使用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.

相关问题 我如何将多个操作结果从家庭控制器传递到index.cshtml视图 - How do i pass multiple actionresults from home controller to index.cshtml view 在同一控制器中的两个ActionResult之间传递数据,从而在ASP.NET MVC中呈现不同的视图 - Passing data between two ActionResults in the same controller rendering different views in asp.net mvc 如何在同一控制器内的操作中传递变量? - How do I pass variables along the actions within the same controller? 使用TempData,Session或静态变量在同一控制器中的ActionResults之间共享相同的值是否更好? - Is it better to use TempData, Session, or a static variable to share the same value between ActionResults in the same controller? 如何在同一控制器内的另一个动作中返回主视图? - How to return the main view in another action within same controller? 如何从同一视图执行3种不同的控制器方法 - How to execute 3 different controller methods from the same view 如何在控制器的相同post方法内订阅并引发事件? - How do I subscribe to and raise an event within the same post method of my controller? 如何将相同的视图模型用于不同的视图(排除必需的属性)? - How do I use same view model but for different view excluding the required properties? 如何在同一控制器方法/视图中多次使用隐藏值? - How do I use a hidden value more than once in the same controller method/view? 如何在同一JSON对象中反序列化不同的NodaTime LocalDate模式 - How do I deserialise different NodaTime LocalDate patterns within the same JSON object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM