osyris zosar

osyris zosar

  • NA
  • 289
  • 24k

Trying to make a viewmodel with custom IdentityRole

Mar 26 2021 9:44 PM
Hello I am trying to create 2 tables one with users wich is already working and one with Roles
 
View: the bottom table is working well,  only the "applicationRoles" is not   
  1. @model Test4.ViewModels.AdminDetailsViewModel  
  2.   
  3. @{  
  4.     ViewData["Title"] = "Index";  
  5.     Layout = "~/Views/Shared/_Layout.cshtml";  
  6.       
  7. }  
  8.   
  9. <h1>Index</h1>  
  10.   
  11. <p>  
  12.     <a asp-action="Create">Create New</a>  
  13. </p>  
  14. <table class="table">  
  15.     <thead>  
  16.         <tr>  
  17.             <th>  
  18.                 @Html.DisplayNameFor(model => model.applicationRoles[0].Id)  
  19.             </th>  
  20.             <th>  
  21.                 @Html.DisplayNameFor(model => model.applicationRoles[0].Name)  
  22.             </th>  
  23.             <th>  
  24.                 @Html.DisplayNameFor(model => model.applicationRoles[0].Description)  
  25.             </th>  
  26.             <th>  
  27.                 @Html.DisplayNameFor(model => model.applicationRoles[0].ConcurrencyStamp)  
  28.             </th>  
  29.   
  30.             <th></th>  
  31.         </tr>  
  32.     </thead>  
  33.     <tbody>  
  34.         @foreach (var item in Model.applicationRoles)  
  35.         {  
  36.         <tr>  
  37.             <td>  
  38.                 @Html.DisplayFor(modelItem => item.Id)  
  39.             </td>  
  40.             <td>  
  41.                 @Html.DisplayFor(modelItem => item.Name)  
  42.             </td>  
  43.             <td>  
  44.                 @Html.DisplayFor(modelItem => item.Description)  
  45.             </td>  
  46.             <td>  
  47.                 @Html.DisplayFor(modelItem => item.ConcurrencyStamp)  
  48.             </td>  
  49.   
  50.             <td>  
  51.                 <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |  
  52.                 <a asp-action="Details" asp-route-id="@item.Id">Details</a> |  
  53.                 <a asp-action="Delete" asp-route-id="@item.Id">Delete</a>  
  54.             </td>  
  55.         </tr>  
  56.         }  
  57.     </tbody>  
  58.      
  59.   
  60.     <table class="table">  
  61.         <thead>  
  62.             <tr>  
  63.                 <th>  
  64.                     @Html.DisplayNameFor(model => model.application[0].FirstName)  
  65.                 </th>  
  66.                 <th>  
  67.                     @Html.DisplayNameFor(model => model.application[0].LastName)  
  68.                 </th>  
  69.                 <th>  
  70.                     @Html.DisplayNameFor(model => model.application[0].Age)  
  71.                 </th>  
  72.                 <th>  
  73.                     @Html.DisplayNameFor(model => model.application[0].Password)  
  74.                 </th>  
  75.                 <th>  
  76.                     @Html.DisplayNameFor(model => model.application[0].UserName)  
  77.                 </th>  
  78.   
  79.                 <th>  
  80.                     @Html.DisplayNameFor(model => model.application[0].Email)  
  81.                 </th>  
  82.                 <th></th>  
  83.             </tr>  
  84.         </thead>  
  85.         <tbody>  
  86.             @foreach (var item in Model.application)  
  87.             {  
  88.                 <tr>  
  89.                     <td>  
  90.                         @Html.DisplayFor(modelItem => item.FirstName)  
  91.                     </td>  
  92.                     <td>  
  93.                         @Html.DisplayFor(modelItem => item.LastName)  
  94.                     </td>  
  95.                     <td>  
  96.                         @Html.DisplayFor(modelItem => item.Age)  
  97.                     </td>  
  98.                     <td>  
  99.                         @Html.DisplayFor(modelItem => item.Password)  
  100.                     </td>  
  101.                     <td>  
  102.                         @Html.DisplayFor(modelItem => item.UserName)  
  103.                     </td>  
  104.   
  105.                     <td>  
  106.                         @Html.DisplayFor(modelItem => item.Email)  
  107.                     </td>  
  108.                     <td>  
  109.                         <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |  
  110.                         <a asp-action="Details" asp-route-id="@item.Id">Details</a> |  
  111.                         <a asp-action="Delete" asp-route-id="@item.Id">Delete</a>  
  112.                     </td>  
  113.                 </tr>  
  114.             }  
  115.         </tbody>  
 Application Role:

  1. public class ApplicationRole : IdentityRole<Guid>  
  2. {  
  3.     public string Description { get; set; }  
  4. }  
viewmodel:

  1. public class AdminDetailsViewModel  
  2. {  
  3.     public List<ApplicationUser> application { get; set; }  
  4.     public List<ApplicationRole> applicationRoles { get; set; }  
  5. }  
Controller:
  1. public async Task<IActionResult> Index()  
  2. {  
  3.   
  4.     var app = from m in _context.Users  
  5.               select m;  
  6.   
  7.     var approle = from m in  _context.UserRoles  
  8.                   select m;  
  9.   
  10.     var Admininfo = new AdminDetailsViewModel  
  11.     {  
  12.   
  13.         application = await app.ToListAsync(),  
  14.         applicationRoles = await approle.ToListAsync()  // I get a error here
  15.     };  
  16.   
  17.     return View(Admininfo);  
  18. }  

Im not really sure how i should handle this 

Answers (2)