0

I am trying build a project while self learning but have been stuck in one place.

I have this class Roles:

namespace IC2021.Models { public partial class Roles { public Roles() { Staffs = new HashSet<Staffs>(); } public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Staffs> Staffs { get; set; } } } 

And another called Staffs:

namespace IC2021.Models { public partial class Staffs { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Location { get; set; } public int? RoleId { get; set; } public string Archived { get; set; } public virtual Roles Role { get; set; } } } 

This is my RolesController:

namespace IC2021.Controllers { public class RolesController : Controller { private readonly ICOctober2021Context _context; public RolesController(ICOctober2021Context context) { _context = context; } // GET: Roles public async Task<IActionResult> Index() { return View(await _context.Roles.ToListAsync()); } public async Task<IActionResult> RolesWithStaffs() { var iCOctober2021Context = _context.Roles.Include(s => s.Staffs); return View(await iCOctober2021Context.ToListAsync()); } } } 

And finally I'm trying to view it from RolesWithStaffs:

<!-- model declaration --> @model IEnumerable<IC2021.Models.Roles> @{ ViewData["Title"] = "RolesWithViewController"; } <h1>RolesWithViewController</h1> <p> <a asp-action="Create">Create New</a> </p> <table class="table"> <thead> <tr> <th> Role </th> <th> Staff Name </th> <th> <th></th> </tr> </thead> <tbody> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Id) </td> <td> @Html.DisplayFor(modelItem => item.Staffs) </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> 

So here in the view when I tried to access from Staffs, I am not able (for example item.Staffs.FirstName, or anything else from Staffs). Whereas I can do it other way, I mean from staffs view I can access Roles.Name or Id).

Can anyone please help me? Any help will be highly appreciated.

1
  • What is the issue? Errors? Expected input/output?CommentedOct 8, 2021 at 13:39

1 Answer 1

0

Your view model looks quite unusual, IMHO you can try this

public async Task<IActionResult> RolesWithStaffs() { var model= await _context.Set<Staffs>().Include(s => s.Role).ToListAsync(); return View(model); } 

and view

 <table class="table"> <thead> <tr> <th> First Name </th> <th> Last Name </th> <th> Role </th> <th> <th></th> </tr> </thead> <tbody> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(item=> item.FirstName) </td> <td> @Html.DisplayFor(item=> item.LastName) </td> <td> @Html.DisplayFor(item => item.Role.Name) </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> 

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.