I've successfully implemented ApplicationUser and Roles into my MVC .NET Core app. However, when I'm returning this to the view it does not populate that a user has been added to a role.
Here is the controllers related part:
[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));
}
This controller is successfully adding or removing the role that is passed to it from the view, which I will show below: (When I check the SQL Table - it has added or removed successfully to the AspNetUserRoles table so the functionality is working - I just can't get it to display appropriately after this.)
@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");}
}
And here the Index view which is incorrectly showing the Roles as 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> 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>
Apologies if I've included too much code - but how do I get the above view to return the item.IsModerator and item.IsOwner as true and not false?
Thank you!
Edit:
See Below for these users index. Both of these users are assigned to role Owner and Moderator via the edit button (which is the first controller posted above). But On returning to the Index page it does not show them as active.
And here is the related Index Controller:
public IActionResult Index()
{
var users = _db.ApplicationUsers.ToList();
return View(users);
}