In my web application In the main view, I want to show separate model data.
For that first I created a view model like this:
public class ProductsViewModel
{
public IEnumerable < CakesViewModel > CakesVM { get; set; }
public IEnumerable < PizzaViewModel > PizzaVM { get; set; }
public IEnumerable < FastFoodsViewModel > FastFoodsVM { get; set; }
}
public class CakesViewModel
{
[Key]
public int ID {get;set;}
[Required]
[Display(Name = "Product Name")]
public string ProductName { get; set; }
[Required]
public decimal ProductPrice { get; set; }
[Required]
public string ProductPicture { get; set; }
[Required]
[Display(Name = "Category Name")]
public int Category_ID { get; set; }
public string Category_Name { get; set; }
[Required]
public bool Is_Customizable { get; set; }
public virtual ICollection < CustomizableFoods > CustomizableFoods { get; set; }
}
public class PizzaViewModel
{
[Key]
public int ID { get; set; }
[Required]
[Display(Name = "Product Name")]
public string ProductName { get; set; }
[Required]
public decimal ProductPrice { get; set; }
[Required]
public string ProductPicture { get; set; }
[Required]
[Display(Name = "Category Name")]
public int Category_ID { get; set; }
public string Category_Name { get; set; }
[Required]
public bool Is_Customizable { get; set; }
public virtual ICollection < CustomizableFoods > CustomizableFoods { get; set; }
}
public class FastFoodsViewModel
{
[Key]
public int ID { get; set; }
[Required]
[Display(Name = "Product Name")]
public string ProductName { get; set; }
[Required]
public decimal ProductPrice { get; set; }
[Required]
public string ProductPicture { get; set; }
[Required]
[Display(Name = "Category Name")]
public int Category_ID { get; set; }
public string Category_Name { get; set; }
[Required]
public bool Is_Customizable { get; set; }
public virtual ICollection < CustomizableFoods > CustomizableFoods { get; set; }
}
In the controller, I'm trying to get separate product lists to separate lists and from that load to the view model and pass the main view model to the view.
Here is my controller code:
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>();
ProductsViewModel allProducts = new 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.CakesVM(productsView); // here is an error
}
In the allProducts.CakesVM(productsView);
I don't see the CakesVM.Add()
option. Need to know a solution for this.
Your first problem is how you assign a value to a property. You don't use the syntax like it was a method. You just assign a value to the property.
Next, your main problem is the fact that you need a list of CakesViewModel to be able to assign a value to that property. Nothing that a single line in Linq couldn't do.
var cakesVMList = Cakes.Select(item => new CakesViewModel
{
ID = item.ID;
ProductPrice = item.ProductPrice;
ProductName = item.ProductName;
Is_Customizable = item.Is_Customizable;
Category_Name = item.FoodCategories.CategoryName;
ProductPicture = item.ProductPicture;
};
allProducts.CakesVM = cakesVMList;
Of course you need to repeat this pattern for the other properties.