Arian Shahalami
Arian Shahalami

Reputation: 1449

grouping results in a view ASP.NET

I'm using ASP.NET Core 2.2. I have 2 models and a viewmodel which injects data to a view. I want to order results based on their productType. Let's make it clear

This is Product model

public class Product
{
     public int ProductID { get; set; }
     public int ProductName { get; set; }
     public string ProductImage { get; set; }
     public int ProductTypeID { get; set; }

     [ForeignKey("ProductTypeID")]
     public virtual ProductType ProductType{ get; set; }
 }

This is ProductType model

public class ProductType
{
    public int ProductTypeID { get; set; }
    public int ProductTypeName { get; set; }
    public string ProductTypeImage { get; set; }
    public string ProductTypeDescription { get; set; 
}

And finally this is DishesViewModel

public class DishesVM
{
     public IEnumerable<ProductType> ProductType { get; set; }
     public IEnumerable<Product> Product { get; set; }
}

In MyController I get data from DB then with automapper, map them to DishViewModel

public class HomeController : Controller
{
    public IActionResult Dishes()
    {
        var productTypes= _context.ProductType.OrderBy(p =>p.ProductTypeID).ToList();
        var products= _context.Products.OrderBy(p => p.ProductID).ToList();

        var DishesVM = new DishesVM();
            DishesVM.ProductType = _mapper.Map<IEnumerable<ProductType>>(productTypes);
            DishesVM.Product = _mapper.Map<IEnumerable<Product>>(products);
    }
}

Now in Dishes View I can have nested foreach

@model DishesViewModel
<div>
   foreach(var pt in Model.ProductType)
   {
      <h1>pt.ProductTypeName</h1>
      foreach(var p in Model.Product)
      {
        p.ProductName
      }
   }
</div>

This works fine but the only problem it has, is it returns all products. but I want each Product Category has its Products In front of its header. This is visual representation of what I want and what I have now.

This is what I want what I want

But this is what I have what I have

Upvotes: 0

Views: 157

Answers (2)

Roman Koliada
Roman Koliada

Reputation: 5082

You have to filter your products by product type in each iteration. At the moment you just display all products for each product type:

<div>
   foreach(var type in Model.ProductType)
   {
      //products which belong to the particular type
      var productsForType = Model.Product.Where(x => x.ProductTypeID == type.ProductTypeID);

      <h1>pt.ProductTypeName</h1>
      foreach(var product in productsForType)
      {
           product.ProductName
      }
   }
</div>

Upvotes: 1

Jake Steffen
Jake Steffen

Reputation: 415

This will give you a jump on the linq statement:

var list = productTypes.Where(x => x.ProductTypeID == 1).Select(x => new Product()
        {
            ProductImage = x.ProductTypeImage,

        }).ToList();

You will need to decide on what to put into the where clause, I'm using ProductTypeID

Upvotes: 0

Related Questions