Asp.Net Core 2.0 RedirectToAction在发布后无法正常工作

问题描述 投票:1回答:1

我是MVC的新手。以下是我在View上所做的事情的要点:

显示标识用户及其分配的用户角色。

克隆用户角色页面部分:

有一个SelectList,最终用户可以选择其他一些身份用户。

显示所选用户的当前用户角色 - 我在部分视图中工作。

拥有克隆用户按钮并单击以删除当前显示的用户的所有用户角色,然后将其添加到分配了SelectList中所选用户的所有角色。

我有一个视图以我想要的方式工作,但有一个例外 - 在更改用户的身份角色后(通过单击克隆用户链接),我的视图部分不会刷新以显示新角色。在我的ViewModel中,这将是UserRoles。如果我按CTRL + F5刷新,那么我会看到最新的角色。

这是我的ViewModel:

public ApplicationUser AppUser { get; set; }
public TblEmployee Employee { get; set; }
[Display(Name = "User Roles")]
public List<Tuple<string, string>> UserRoles { get; set; }
[Display(Name = "Available Roles")]
public List<Tuple<string, string>> AllRoles { get; set; }
[Display(Name = "Select Employee")]
public List<SelectListItem> EmployeeList { get; set; }

我的页面上有一个链接:

<a id="uxCloneUserButton" class="hidden">
    <i class="glyphicon glyphicon-duplicate"></i>  &nbsp;Clone User
</a>

然后在我的View中我的脚本中有以下内容:

$('#uxCloneUserButton').click(function () {
    var selectedId = $('#uxEmployeeSelect').val();
    var appUserId = $('#uxHiddenAppUserId').val();
    var url = '@Url.Action("CloneUser", "ApplicationUser")';

    //post id and username to controller action
    $.post(url, { id: appUserId, cloneUserId: selectedId }, function (data) {
    });
});

我使用适当的参数进入控制器上的Action,它运行正常。在我的控制器中我有这个:

[HttpPost]
public async Task<IActionResult> CloneUser(string id, string cloneUserId)
{   
    ApplicationUser currentUser = await _userManager.FindByIdAsync(id);
    ApplicationUser cloneUser = await _userManager.FindByIdAsync(cloneUserId);
    //Remove currentUser from all current roles
    var currentUserRoles = await _userManager.GetRolesAsync(currentUser);
    if (currentUserRoles.Count > 0)
        await _userManager.RemoveFromRolesAsync(currentUser, currentUserRoles.ToArray());
    var cloneUserRoles = await _userManager.GetRolesAsync(cloneUser);
    if (cloneUserRoles.Count > 0)
        await _userManager.AddToRolesAsync(currentUser, cloneUserRoles.ToArray());               
    return RedirectToAction(nameof(Details), new { id = id });            
}

非常感谢您的帮助。我只需要在点击后刷新页面的这一部分就可以了。

UPDATE

我能够让它工作,但我的解决方案并不像一个好的解决方案。

我在ViewModel中添加了一个字符串:

public string JSRedirectLocation { get; set; }

我在Details Action中设置了属性,我在其中分配了所有VM属性:

//Redirect location
model.JSRedirectLocation = "/ApplicationUser/Details/" + model.AppUser.Id;

我在帖子后改变了我的JavaScript重定向:

//post id and username to controller action
$.post(url, { id: appUserId, cloneUserId: selectedId }, function (data) {
    window.location = '@Model.JSRedirectLocation';
});

有人可以告诉我,我应该这样做一些更干净的方式,拜托?在我看来,这个解决方案是多余的,感觉就像一个解决方法。谢谢。

asp.net asp.net-mvc asp.net-core
1个回答
2
投票

你的请求是ajax调用,为此你必须返回json然后处理响应重定向到target.So in action use:

[HttpPost]
public async Task<IActionResult> CloneUser(string id, string cloneUserId)
{   
    //.. your code

    var redirectUrl = Url.Action(nameof(Details), new { id = id });

    return Json(new {
        redirectUrl = redirectUrl
    }); 
}

并在ajax调用中使用以下代码:

$.post(url, { id: appUserId, cloneUserId: selectedId }, function (data) {
    window.location = data.redirectUrl;
});
© www.soinside.com 2019 - 2024. All rights reserved.