Reputation: 498
I am currently learning .NET Core and I'm trying to make a user management using Identity framework in ASP.NET Core MVC 3.1. In the Controllers
I have a RoleManagerController
with EditRole
GET and POST like this:
[HttpGet]
public async Task<IActionResult> EditRole(string roleId)
{
ViewBag.roleId = roleId;
var role = await _roleManager.FindByIdAsync(roleId);
if (role == null)
{
ViewBag.ErrorMessage = $"Role with RoleID = {roleId} doest not exists.";
return NotFound();
}
// create new roleModel
var roleModel = new EditRoleViewModel
{
RoleId = role.Id,
RoleName = role.Name
};
// retrieve all users based on current role
foreach (var user in _userManager.Users.ToList())
{
if (await _userManager.IsInRoleAsync(user, role.Name))
{
roleModel.Users.Add(user.UserName);
}
}
return View(roleModel);
}
[HttpPost]
public async Task<IActionResult> EditRole(EditRoleViewModel roleModel)
{
var role = await _roleManager.FindByIdAsync(roleModel.RoleId);
if (role == null)
{
ViewBag.ErrorMessage = $"Role with RoleId = {roleModel.RoleId} does not exists.";
return NotFound();
}
else
{
role.Name = roleModel.RoleName;
// update the role
var updateStatus = await _roleManager.UpdateAsync(role);
// check while updating is succeed or not
if (updateStatus.Succeeded)
{
return RedirectToAction("Index");
}
foreach (var error in updateStatus.Errors)
{
ModelState.AddModelError("updating failed", error.Description);
}
}
return View(roleModel);
}
Based on EditRole
GET, I created a view named EditRole.cshtml
in Views/RoleManager
folder, like this:
@model EditRoleViewModel
@{
ViewData["Title"] = "Edit Role";
}
<h4>Edit Role</h4><br />
<form class="" method="post">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="RoleId" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="RoleId" class="form-control" disabled/>
</div>
</div>
<div class="form-group">
<label asp-for="RoleName" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="RoleName" class="form-control"/>
<span asp-validation-for="RoleName" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<button type="submit" class="btn btn-primary">Update</button>
<a asp-action="Index" class="btn btn-secondary">Cancel</a>
</div>
</div>
</form>
This is something that I configured in Startup.cs
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
But when I try to edit role and access the route with role's id the page said no webpage was found for the web address.
. Am I missing something?
Upvotes: 1
Views: 2009
Reputation: 498
Just for a moment I decided to check all the script those are related to the problem route and I realized that the tag helper I provided in Edit button in the Index.cshtml which link to the route was set to [email protected] which will not work, then I try to change it to [email protected] , the roleId is what I defined in the RoleManagerViewModel. Now it worked well.
Upvotes: 0
Reputation: 51
The problem may be that you do not specify Area !
if you put this section into Separate Area you must Definition one new route at startup.cs same this :
routes.MapRoute(
name: "areas",
template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
);
Upvotes: 1