In my web application, I want to show in the view item list separated by their Category_ID.
To do that In my view model I created
public class ProductsViewModel
{
public IEnumerable<CakesViewModel> CakesVM { get; set; }
public IEnumerable<PizzaViewModel> PizzaVM { get; set; }
public IEnumerable<FastFoodsViewModel> FastFoodsVM { get; set; }
}
In the controller
public ActionResult Index()
{
List<Products> Cakes = db.Products.Where(x=>x.Category_ID == 1).ToList<Products>();
List<Products> Pizzas = db.Products.Where(x => x.Category_ID == 2).ToList<Products>();
List<Products> FastFoods = db.Products.Where(x => x.Category_ID == 3).ToList<Products>();
List<ProductsViewModel> allProducts = new List<ProductsViewModel>();
foreach (Products item in Cakes)
{
CakesViewModel productsView = new CakesViewModel();
productsView.ID = item.ID;
productsView.ProductPrice = item.ProductPrice;
productsView.ProductName = item.ProductName;
productsView.Is_Customizable = item.Is_Customizable;
productsView.Category_Name = item.FoodCategories.CategoryName;
productsView.ProductPicture = item.ProductPicture;
allProducts.Add(productsView);
}
foreach (Products item in Pizzas)
{
PizzaViewModel productsView = new PizzaViewModel();
productsView.ID = item.ID;
productsView.ProductPrice = item.ProductPrice;
productsView.ProductName = item.ProductName;
productsView.Is_Customizable = item.Is_Customizable;
productsView.Category_Name = item.FoodCategories.CategoryName;
productsView.ProductPicture = item.ProductPicture;
allProducts.Add(productsView);
}
foreach (Products item in FastFoods)
{
FastFoodsViewModel productsView = new FastFoodsViewModel();
productsView.ID = item.ID;
productsView.ProductPrice = item.ProductPrice;
productsView.ProductName = item.ProductName;
productsView.Is_Customizable = item.Is_Customizable;
productsView.Category_Name = item.FoodCategories.CategoryName;
productsView.ProductPicture = item.ProductPicture;
allProducts.Add(productsView);
}
return View(allProducts);
}
I want to know, here I can't add Items in this way allProducts.Add(productsView)
Want to know how to add Cakeslist to the CakesViewModel and Pizza to PizzaViewModel and pass to the View.