Ok, I'm new to .NET at all. (Some Programming knowledge)
- I created a new .NET CORE project.
- Added 4 Roles to the AspNetRoles DataBase
- In the AccountController, on the Register[POST] action added this for all users to have a rol:
await _userManager.AddToRoleAsync(user, "Llamada"); //"llamada" is the base rol for everyone
- Added some 'fileds' to the AplicationUser.cs Model
- Scaffold-ed the AplicationUser.cs Model
- Modified the Index and Edit Views to show Rol:
//Index view @model IEnumerable<Avivamiento.Models.ApplicationUser> @{ ViewData["Title"] = "Index"; }<h2>Index</h2><p><a asp-action="Create">Create New</a></p><table class="table"><thead><tr><th> @Html.DisplayNameFor(model => model.CC)</th><th> @Html.DisplayNameFor(model => model.Nombre)</th><th> @Html.DisplayNameFor(model => model.Apellido)</th><th>Rol</th><th></th></tr></thead><tbody> @foreach (var item in Model) {<tr><td> @Html.DisplayFor(modelItem => item.CC)</td><td> @Html.DisplayFor(modelItem => item.Nombre)</td><td> @Html.DisplayFor(modelItem => item.Apellido)</td><td> @Html.DisplayFor(modelItem => item.Roles)</td><td><a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |<a asp-action="Details" asp-route-id="@item.Id">Details</a> |<a asp-action="Delete" asp-route-id="@item.Id">Delete</a></td></tr> }</tbody></table>
//Edit view
@model Avivamiento.Models.ApplicationUser
@{
ViewData["Title"] = "Edit";
}<h2>Edit</h2><form asp-action="Edit"><div class="form-horizontal"><h4>ApplicationUser</h4><hr /><div asp-validation-summary="ModelOnly" class="text-danger"></div><input type="hidden" asp-for="Id" /><div class="form-group"><label asp-for="CC" class="col-md-2 control-label"></label><div class="col-md-10"><input asp-for="CC" class="form-control" /><span asp-validation-for="CC" class="text-danger" /></div></div><div class="form-group"><label asp-for="Nombre" class="col-md-2 control-label"></label><div class="col-md-10"><input asp-for="Nombre" class="form-control" /><span asp-validation-for="Nombre" class="text-danger" /></div></div><div class="form-group"><label asp-for="Apellido" class="col-md-2 control-label"></label><div class="col-md-10"><input asp-for="Apellido" class="form-control" /><span asp-validation-for="Apellido" class="text-danger" /></div></div><div class="form-group"><label asp-for="Roles" class="col-md-2 control-label"></label><div class="col-md-10"><input asp-for="Roles" class="form-control" /><span asp-validation-for="Roles" class="text-danger" /></div></div><div class="form-group"><div class="col-md-offset-2 col-md-10"><input type="submit" value="Save" class="btn btn-default" /></div></div></div></form><div><a asp-action="Index">Back to List</a></div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
- Successfully created an user and confirmed on the AspNetUserRoles the ids for my user and the rol I 'enforced'
But on the Index view the rol is empty, and the Edit view the Input control for Rol gets:
System.Collections.Generic.List[Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserRo[System.String]]
So I don't see the Rol asigned. I tried different combinations to try to solve. i.e. in the Index view tried
@Html.DisplayFor(modelItem => item.Roles.SingleOrDefault())
But get a different error and so on.
How can I get the rol the user has?
Thanks.