2016-12-04 9 views
0

インデックスビューにロールリストを表示しようとしていますが、dbo.AspNetRolesにアクセスする方法がないようですね??asp.net mvcのIdentityUserからビューのロールの名前を取得するには?

ROLEメンテナンスCONTROLLER:

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); 
     } 
    } 
} 

アプリケーションユーザモデル

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; } 
    } 
} 

インデックスの役割メンテナンス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> 

私はitem.RolesからRoles.NameまたはRoles.Idにアクセスすることはできません。アプリケーションのユーザーにvirtual RoleNavigationプロパティを作成しようとしましたが、自分でIdentityRole.csというモデルを作成しようとしましたが、多くのことを試しました。誰でも助けてくれますか?私はこのコードを実行すると、私はこのエラーを取得されるよう:

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); 

答えて

0

読むこの最初:あなたは、この本に似た何かを行っている場合はhttp://www.c-sharpcorner.com/UploadFile/asmabegam/Asp-Net-mvc-5-security-and-creating-user-role/

ユーザーに関連付けられたロールを取得し、利用可能なすべてのロールのリストを取得するために私がやっていることです:

public ActionResult GetRoles(string UserName) 
    { 
     if (!string.IsNullOrWhiteSpace(UserName)) 
     { 
      Models.ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault(); 
      var account = new AccountController(HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(), null); 

      ViewBag.RolesForThisUser = account.UserManager.GetRoles(user.Id); 

      // prepopulate roles for the view dropdown 
      var list = context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList(); 
      ViewBag.Roles = list; 
     } 

     return View("ManageUserRoles"); 
    } 

おそらくこのコードはあなたにいくつかのアイデアを与えるでしょう:)....私は、結果を含むためにViewModelを作成していたはずです...しかし、私はかわりにViewBagを使用しました。見る側では

、ここにリストを使用したコードは次のとおりです。

 @Html.AntiForgeryToken() 
     @Html.ValidationSummary(true) 
     <div class="col-sm-6" > 
       <h3>Add Role to @Model.UserName</h3> 
       @Html.Hidden("UserID", Model.Id) 
      <span style="font-size:large">Role Name:</span> @Html.DropDownList("RoleName", (IEnumerable<SelectListItem>)ViewBag.Roles, "Select ...") 
      <input class="btn-sm btn-default" type="submit" value="Save" /> 
     </div> 
+0

はあなたの助けをありがとう、私は単にあなたがあなたのビューと一緒にこれを使用する方法を理解していませんか? –

関連する問題