成功添加角色后,将用户角色返回到视图

时间:2019-07-08 01:42:07

标签: c# asp.net-core

我已经在我的MVC .NET Core应用程序中成功实现了ApplicationUser和Roles。但是,当我将其返回到视图时,它并不表示已将用户添加到角色中。

这是控制器相关部分:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit (string id, ApplicationUser applicationUser)
{
    if (id != applicationUser.Id)
    {
        return NotFound();
    }
    if (ModelState.IsValid)
    {
        ApplicationUser userFromDb = _db.ApplicationUsers.Where(u => u.Id == id).FirstOrDefault();

        userFromDb.FirstName = applicationUser.FirstName;
        userFromDb.LastName = applicationUser.LastName;

        if (applicationUser.IsModerator)
        {
            await _userManager.AddToRoleAsync(userFromDb, SD.ModeratorEndUser);
        }
        if (!applicationUser.IsModerator)
        {
            await _userManager.RemoveFromRoleAsync(userFromDb, SD.ModeratorEndUser);
        }

        if (applicationUser.IsOwner)
        {
            await _userManager.AddToRoleAsync(userFromDb, SD.OwnerEndUser);
        }
        if (!applicationUser.IsOwner)
        {
            await _userManager.RemoveFromRoleAsync(userFromDb, SD.OwnerEndUser);
        }

        _db.SaveChanges();
        return RedirectToAction(nameof(Index));
    }

此控制器正在成功添加或删除视图中传递给它的角色,这将在下面显示:(当我检查SQL表时-它已成功添加或删除了AspNetUserRoles表,因此该功能正常运行-在此之后我无法正常显示。)

@model Sentimented.Models.ApplicationUser

@{
    ViewData["Title"] = "Edit";
}
<br />
<h2 class="text-info">Edit User Roles</h2>

<form method="post" asp-action="Edit">
    <div class="p-4 border rounded">
        <input type="hidden" asp-for="Id" />
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group row">
            <div class="col-2">
                <label asp-for="FirstName"></label>
            </div>
            <div class="col-5">
                <input asp-for="FirstName" class="form-control" />
            </div>
            <span asp-validation-for="FirstName" class="text-danger"></span>
        </div>
        <div class="form-group row">
            <div class="col-2">
                <label asp-for="LastName"></label>
            </div>
            <div class="col-5">
                <input asp-for="LastName" class="form-control" />
            </div>
            <span asp-validation-for="LastName" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="IsModerator"></label>
            <input asp-for="IsModerator" type="checkbox" class="form-control" />
        </div>
        <div class="form-group">
            <label asp-for="IsOwner"></label>
            <input asp-for="IsOwner" type="checkbox" class="form-control" />
        </div>
        <br />
        <div class="form-group">
            <input type="submit" class="btn btn-primary" asp-route-id="@Model.Id " value="Update" />
            <a asp-action="Index" class="btn btn-success">Back to List</a>
        </div>
    </div>
</form>

@section Scripts{
    @{ await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

在这里是索引视图,它错误地将Roles显示为false。

      @model IEnumerable<Sentimented.Models.ApplicationUser>

@{
    ViewData["Title"] = "Index";
}
<br />

<div class="row">
    <div class="col-6">
        <h2 class="text-info">User List</h2>
    </div>

    <div class="col-6 text-right">
        <a asp-action="New" class="btn btn-info"><i class="fas fa-plus"></i>&nbsp; New Text</a>
    </div>
</div>

<br />

<div>
    <table class="table table-striped border">
        <tr class="table-info">
            <th>
                @Html.DisplayNameFor(m => m.FirstName)
            </th>
            <th>
                @Html.DisplayNameFor(m => m.LastName)
            </th>
            <th>
                @Html.DisplayNameFor(m => m.Email)
            </th>
            <th>
                @Html.DisplayNameFor(m => m.IsOwner)
            </th>
            <th>
                @Html.DisplayNameFor(m => m.IsModerator)
            </th>
            <th>
                Edit
            </th>
            <th>
                Details
            </th>
        </tr>

        @foreach (var item in Model)
        {
    <tr>
        <td>@Html.DisplayFor(m => item.FirstName)</td>
        <td>@Html.DisplayFor(m => item.LastName)</td>
        <td>@Html.DisplayFor(m => item.Email)</td>
        <td>@Html.DisplayFor(m => item.IsModerator)</td>
        <td>@Html.DisplayFor(m => item.IsOwner)</td>

        <td>
            <a type="button" class="btn btn-primary" href="@Url.Action("Edit/" + item.Id)">
                <i class="fas fa-edit"></i>
            </a>
        </td>
        <td>
            <a type="button" class="btn btn-primary" href="@Url.Action("Details/" + item.Id)">
                <i class="fas fa-edit"></i>
            </a>
        </td>
    </tr>
        }
    </table>
</div>

很抱歉,如果我包含了太多的代码-但是如何获得以上视图以将item.IsModerator和item.IsOwner返回为true而不是false?

谢谢!

编辑:

请参阅下面的这些用户索引。通过编辑按钮(这是上面发布的第一个控制器)将这两个用户都分配给角色所有者和主持人。但是返回索引页面时,它不会显示为活动状态。

Index of User Page

这是相关的索引控制器:

public IActionResult Index()
{
    var users = _db.ApplicationUsers.ToList();

    return View(users);
}

1 个答案:

答案 0 :(得分:0)

由于用户已经登录,因此等待*(q[0])的这一行代码将更新db,但不会更新cookie。您必须重新发出Cookie:

因此,在type(of: a).x 之后,您可以使用_userManager.AddToRoleAsync(userFromDb, SD.OwnerEndUser);

您需要像这样注入_db.SaveChanges();

_signInManager.RefreshSignInAsync(userFromDb)

并刷新用户的cookie:

SignInManager