Display table with category name in one column and list of items belonging to category in second column with links on Details View in ASP.NET MVC View

Viewed 437

I my ASP.NET MVC Core 3.1 app I have Model that stores professions which looks like this:

public partial class Professions
{
    public int ProfessionID { get; set; }
    public int? ProfessionGroupID { get; set; }
    public string ProfessionTitle { get; set; }
    public string ProfessionDescription { get; set; }
}

So each profession belongs to some group of professions, therefore Model for profession groups looks like this:

public partial class ProfessionGroups
{
    public ProfessionGroups()
    {
        Professions = new HashSet<Professions>();
    }

    public int ProfessionGroupID { get; set; }
    public int ProfessionGroupTitle { get; set; }
    public string ProfessionGroupDescription { get; set; }

    public virtual ICollection<Professions> Professions { get; set; }
}

How can I display list of groups of professions in one columns and list of professions in another column with links which leads to each profession Details View? This is how output table in some View should look:

So far I've created ViewModel that suppose to hold needed data:

public class ProfGroupsVM
{
    public int ProfessionGroupID { get; set; }
    public string ProfessionGroupTitle { get; set; }
    public List<int> ProfIDs { get; set; } = new List<int>();
    public List<string> ProfTitles { get; set; } = new List<string>();

}   

For each ProfessionGroupID it has to populate ProfIDs with list of professions belonging to that group and ProfTitles should hold professions titles.

So in Controller so far I have:

   var profGroupsVM= new ProfGroupsVM();

   var result = from p in _context.Professions
        group p.ProfessionID by p.ProfessionGroupID into g
                select new  { ProfessionGroupID = g.Key, ProfessionIDs= g.ToList() };

I don't know how to assign this result back to my ViewModel nor how to display desired View.

1 Answers

It's better to change the ProfGroupsVM model to this:

public class ProfGroupsVM
    {
        public int ProfessionGroupID { get; set; }
        public string ProfessionGroupTitle { get; set; }
        public List<ProfInfo> Professions { get; set; }

        public class ProfInfo
        {
            public int Id { get; set; }

            public string Title { get; set; }
        }
    }

And to get information use this:

   _dbContext.ProfessionGroups.Include(x => x.Professions)
              .Select(x => new ProfGroupsVM()
              {
                  ProfessionGroupID = x.ProfessionGroupID,
                  ProfessionGroupTitle = x.ProfessionGroupTitle,
                  Professions = x.Professions.Select(p => new ProfGroupsVM.ProfInfo
                  {
                      Id = p.ProfessionID,
                      Title = p.ProfessionTitle
                  }).ToList()
              }).ToList();

And in you view try this to show information(I'm not good in UI :)):

@model List<ProfGroupsVM>

 <div>
  <ul>
      @foreach (var group in Model)
      {
        <li>@group.ProfessionGroupTitle</li>
        <ul>
            @foreach (var profession in group.Professions)
            {
                <li><a href="@profession.Id">@profession.Title</a></li>
            }
        </ul>

       }
   </ul>

Or if you stick with your ViewModel for ul part you can put this:

<ul>
@foreach (var idsTitles in item.ProfIDs.Zip(@item.ProfTitles, Tuple.Create)) 
{
<li><a asp-action="Details" asp-controller="Profession" asp-route-id="@idsTitles.Item1">@idsTitles.Item2</a></li>
}
</ul>
Related