我该如何做...查看MVC 5上的多个模型 [英] How Can I do this ...Multiples models on view MVC 5
问题描述
我要执行此视图..我有一个模型调用Product Options,我需要在视图中显示不同表的名称或照片路径
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; }
}
}
这是产品型号:
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模型
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; }
}
选项模型
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.
我一直创建一个 ViewModel ,其中包含我在视图中所需的每个模型的对象.例如,如果您需要三个模型:Product
,Option
和OptionsGroup
,我将这样创建一个ViewModel:
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; }
}
当您希望访问任何ProductOptionsVM
对象时,必须记住使用访问器(就像使用常规Model一样)来操纵Model对象的每个属性,如下所示:
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 */
}
注意:作为一种好的做法,创建任何ViewModel时,后缀应为VM或ViewModel.
Note: As a good practice when you create any ViewModel, it should have a suffix as VM or ViewModel.
这篇关于我该如何做...查看MVC 5上的多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!