Ich versuche, eine Liste von Rollen in meiner Indexansicht anzuzeigen, aber es scheint keine Möglichkeit zu geben, auf dbo.AspNetRoles zuzugreifen ??Wie Name der Rollen in Ansichten von IdentityUser in asp.net mvc erhalten?
ROLLE CONTROLLER MAINTENANCE:
namespace DwBusService.Controllers
{
public class DwRoleMaintenanceController : Controller
{
// private readonly ApplicationDbContext _context;
private readonly RoleManager<IdentityRole> roleManager;
private readonly UserManager<ApplicationUser> userManager;
public DwRoleMaintenanceController(RoleManager<IdentityRole> roleManager, UserManager<ApplicationUser> userManager)
{
this.roleManager = roleManager;
this.userManager = userManager;
}
// GET: DwRoleMaintenance
public async Task<IActionResult> Index()
{
var roles = roleManager.Roles.OrderBy(a => a.Name);
return View(roles);
}
}
}
Anwendungsbenutzer MODEL
namespace DwBusService.Models
{
// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUser : IdentityUser
{
public ApplicationUser()
{
}
[Display(Name = "Local Authentication")]
public bool LocalAuthentication { get; set; }
}
}
INDEX Rollenpflege VIEW
@model IEnumerable<DwBusService.Models.ApplicationUser>
....
<table class="table">
<thead>
<tr>
<th>@Html.DisplayNameFor(model => model.Roles)</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<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>
ich nicht Roles.Name
oder Roles.Id
von item.Roles
zugreifen können. Ich habe versucht, eine virtual RoleNavigation
Eigenschaft in Anwendung Benutzer zu erstellen, habe ich versucht, mein eigenes IdentityRole.cs
Modell zu machen, habe ich viele Dinge ausprobiert. Kann mir jemand helfen? Wenn ich diesen Code ausführen wie bekomme ich diesen Fehler:
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable'1[Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable'1[DwBusService.Models.ApplicationUser]'.
DBO.ASPNETROLES.SQL
CREATE TABLE [dbo].[AspNetRoles] (
[Id] NVARCHAR (450) NOT NULL,
[ConcurrencyStamp] NVARCHAR (MAX) NULL,
[NormalizedName] NVARCHAR (256) NULL,
CONSTRAINT [PK_AspNetRoles] PRIMARY KEY CLUSTERED ([Id] ASC)
);
GO
CREATE NONCLUSTERED INDEX [RoleNameIndex]
ON [dbo].[AspNetRoles]([NormalizedName] ASC);
Vielen Dank für Ihre Hilfe, aber ich verstehe einfach nicht, wie Sie dies mit Ihrer Sicht in Verbindung? –