0

I have three kind of products showing on my index view divided in 3 slots like this

<div class="panel panel-body">
    <div class="col-md-4 table-responsive" id="carTable"></div>
    <div class="col-md-4 table-responsive" id="boatTable"></div>
    <div class="col-md-4 table-responsive" id="otherProductsTable"></div>
</div>

and working great. Departments like Ny, Dubai .. are dynamic and this is my class:

public class Departments
{
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int DeptId { get; set; }
        public string DeptName { get; set; }
}

But now I am trying to show a summary of my products including department they belong to, like this screenshot below shows:

enter image description here

By coding like this:

public async Task<ActionResult> GetProductSummary() 
{ 
     using (context) 
     { 
          return (PartialView("_ProductSummary", await context.Inventories.Include(b =>b.KindOfProducts).GroupBy(m => m.KindOfProducts.ProductType).Select(g => new InventoryModel { BetName = g.Key, Status = g.Sum(c => c.Status) }).ToListAsync())); 
     } 
 } 

I got a result like this shown in this screenshot:

enter image description here

But what I really want is like this:

enter image description here

I do not know How to change my Linq query and before that how to create a good structured ViewModel. Right now I have this incomplete ViewModel:

public class InventoryModel 
{ 
     // I don't know if I need all this properties... 
     public KindOfProducts ProductType { get; set; } 
     public string ProductType { get; set; } 

     public int KindOfProductId { get; set; } 

     public Department department { get; set; } 

     public string DeptName { get; set; } 
     public int DeptId { get; set; } 

     public int Status { get; set; } 
 }

Thank you in advance!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Helen Tekie
  • 515
  • 1
  • 6
  • 23
  • The List of lities, like "NY, Dubai, Paris", is this list very dynamic (e.g. could be a range of 0-n Cities for any request) or is it a list which won't change? For the Query, it's getting a bit complex, what you need is a "PIVOT". This could be interesting: https://stackoverflow.com/questions/167304/is-it-possible-to-pivot-data-using-linq – thmshd Dec 19 '17 at 21:10
  • You need a view model that represents your table layout. It will contain a collection of view models representing a table row, and that view model will contain a properties representing the table columns –  Dec 19 '17 at 21:11
  • @thmshd thank you for your response, Yes departments are Dynamics. I have now edited my question with Department class – Helen Tekie Dec 19 '17 at 21:22
  • @StephenMuecke thank you Stephen, do you mean somethink like this in my modelView public List departments { get; set; } = new List(); ? – Helen Tekie Dec 19 '17 at 21:23
  • No. You view model(s) need to be along the lines of [this answer](https://stackoverflow.com/questions/40406540/creating-html-table-dynamically-with-some-complexity/40435167#40435167) and in you view use use nested loops to generate your `` and `` elements. And then your query will include `.GroupBy()` clauses. –  Dec 19 '17 at 21:27

0 Answers0