2016-04-11 16 views
1

Bu görünümü görmek istiyorum .. Modelli bir ürün var Ürün Seçenekleri ve görünümüne farklı tablolar için ad veya fotoğraf yolu getirmem gerekiyorBunu nasıl yapabilirim ... Çoklu modeller modeller bakış MVC 5

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 Modeli

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

    } 
    } 

Bu

enter image description here

Ürün Modeli vardır

Seçenekler Modeli Anladığım kadarıyla

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

cevap

1

, tek Görünümü içinde üç farklı modeller erişmesini istiyorum. Bu, number of different ways aracılığıyla mümkündür.

Her zaman görünümümün gerektirdiği her bir Model I'in nesnelerini içeren bir ViewModel oluşturdum. Eğer üç model gerektiriyorsa Örneğin,: Product, Option ve OptionsGroup, çok gibi bir ViewModel yaratacak: Herhangi ProductOptionsVM nesnelere erişmek istediğinizde

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

, size olduğu gibi (erişenleri kullanmayı unutmamalıyız düzenli Modeli) şöyle Modeli nesnenin her özelliği işlemek için:

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 */ 
} 

Not: herhangi ViewModel oluşturmak iyi bir uygulama olarak, bu VM veya ViewModel olarak bir sonek olmalıdır.

İlgili konular