How Can I do this …Multiples models on view MVC 5

2019-07-27 15:38发布

问题:

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; }
    }

回答1:

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 and OptionsGroup, I would create a ViewModel as so:

public class ProductOptionsVM
{
    public Product product { get; set; }
    public Option options { get; set; }
    public OptionsGroup optiongroup { get; set; }
}

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:

public ViewResult setProductDetails(ProductOptionsVM poViewModel)
{
    poViewModel.product.Id = 1;
    poViewModel.product.Name = "MyProductName";
    poViewModel.product.CategoryId = 1;
    poViewModel.product.Price = 123.45m;
    poViewModel.product.PhotoPath = "C:\Users\Marimar\Desktop\images\myimage.jpg"
    poViewModel.product.DiscountId = 1;
    poViewModel.product.Status = false;
    poViewModel.product.ProductOptions = _productService.GetAllProductOptions();

    return View(obj); /* Your product model will be initialized,
                         whilst the Options and OptionGroup model is null */
}

Note: As a good practice when you create any ViewModel, it should have a suffix as VM or ViewModel.