I want to do this view .. I have a model call Product Options and I need to bring on the view the name or photo path for different tables
public class ProductOption
{
public int id { get; set; }
[Display(Name = "Product")]
public int ProductId { get; set; }
[Display(Name = "Group")]
public int GroupId { get; set; }
[Display(Name = "Option")]
public int OptionId { get; set; }
[Display(Name = "Price")]
public decimal priceIncrement { get; set; }
}
}
This are the Product Model:
public class Product
{
public int Id { get; set; }
[Display(Name = "Product Name")]
public string Name { get; set; }
[Display(Name = "Category")]
public int CategoryId { get; set; }
[Column(TypeName = "Money")]
public decimal Price { get; set; }
[Display(Name = "Image")]
public string PhotoPath { get; set; }
[Display(Name = "Discount")]
public int DiscountId { get; set; }
[Display(Name = "Enable")]
public bool status { get; set; }
public virtual ICollection<ProductOption> ProductOptions { get; set; }
}
OptionsGroup Model
public class OptionsGroup
{
public int OptionsGroupId { get; set; }
[Display(Name = "Group Name")]
public string OptionsGroupName { get; set; }
public virtual ICollection<Option> Options { get; set; }
}
Options Model
public class Option
{
public int OptionId { get; set; }
[Display(Name="Option Name ")]
public string OptionName { get; set; }
[Display(Name = "Group Name ")]
public int OptionsGroupId { get; set; }
public virtual OptionsGroup OptionsGroup { get; set; }
}
From what I understand, you want to have access to three different Models within a single View. This is possible via a number of different ways.
I've always created a ViewModel that contains objects of each individual Model I require in my view. For example, If you require three models:
Product
,Option
andOptionsGroup
, I would create a ViewModel as so:When you wish to access any
ProductOptionsVM
objects, you must remember to use the accessors (as you would a regular Model) to manipulate the each property of Model object like so:Note: As a good practice when you create any ViewModel, it should have a suffix as VM or ViewModel.