2017-01-01 72 views
0

我devolaping一個二手物品出售就像OLX,我有兩個型號在單個視圖ASP.NET使用多個模型,MVC

1:Product_model

2:Customer_model

所以我想在一個視圖中使用產品描述和客戶信息。 型號:

[Table("Product")] 
    public class ProductModel 
    { 
     [Key] 
     public string ProductId { get; set; } 
     public string ProductName { get; set; } 
     public decimal ProductPrice { get; set; } 
     public string ProductDescription { get; set; } 
     public string ProductCategory { get; set; } 
     public string ProductAddress { get; set; } 
     public string ProductImage1 { get; set; } 
     public string ProductImage2 { get; set; } 
     public string ProductImage3 { get; set; } 
     public string CustomerId { get; set; } 
     // public DateTime ProductSubTime { get; set; } 
    } 

    [Table("Customer")] 
    public class CustomerModel 
    { 
     [Key] 
     [Required(ErrorMessage = "Please provide Customer ID", 
     public string CustomerFullName { get; set; } 
     public int CustomerContact { get; set; } 
     public string CustomerEmail { get; set; } 
     public string CustomerGender { get; set; } 
     public string CustomerAddress { get; set; } 
    } 

我的控制器:

public ActionResult Index() 
    { 
     return View(cm.Products.ToList()); 
    } 

我的觀點:

@model IEnumerable<WebApplication11.Models.ProductModel> 
@{ 
    ViewBag.Title = "Index"; 
    Layout = "~/Views/Shared/MyTemplate.cshtml"; 
} 

<div class="col-md-4"style="border: 2px solid yellow" > 
       <div class="single-product-widget" style="border: 2px solid black"> 
        <h2 class="product-wid-title">Recently Viewed</h2> 
        <a href="#" class="wid-view-more">View All</a> 
        @{ int i = 0;} 
        @foreach (var item in Model) 
        { 
          <div class="single-wid-product" style="border: 2px dashed black"> 
          <a href="single-product.html"><img src="~/images/@item.ProductImage1" alt="No Photo" class="product-thumb"></a> 
          <h2>@Html.DisplayFor(model => item.ProductName)</h2> 
          </div> 
        } 
      <div> 
</div> 

爲了理解的目的我刪除的視圖某些部分。 請指導我在特定的視圖中訪問兩個模型。 我也嘗試了一些方法,但無法得到它。 在此先感謝...

回答

0

你可以像下面的代碼

public class MyModel 
{ 
    public List<ProductModel> Products { get; set; } 
    public List<CustomerModel> Customers { get; set; } 
} 

在控制器創建2個屬性視圖模型:

public ActionResult Index() 
    { 
     var model = new MyModel 
     { 
      Products = cm.Products.ToList(), 
      Customers = cm.Customers.ToList() 
     }; 
     return View(model); 
    } 

在教職員

@foreach (var item in Model.Products) 
{ 
    @*Write your razor code here*@ 
} 
+0

Thnks!請給我發送視圖編碼,使用'@foreach loop()'訪問兩個模型 –

+0

@foreach(模型中的var項) –

+0

foreach(Model.Products中的var項) –

1

有幾個選項可供選擇在一個特定的視圖中的幾個模型。我假設你只是想在頁面角落顯示一些客戶信息。您的佈局視圖可能需要包含此客戶信息(但無論如何,這並不重要)。從我的角度來看

  1. 最優選的選擇是使用子操作

創建CustomerController

public class CustomerController : Controller { 
    [ChildActionOnly] 
    public ViewResult CustomerInfo() { 
     CustomerModel customer = ... 
     return PartialView("_CustomerPartial", customer); 
    } 
} 

.chstml視圖將要顯示的客戶信息@Html.Action("CustomerInfo", "Customer")

這樣你將有一個單獨的CustomerModel創建邏輯。

  1. 另一種選擇是使用ViewDataViewBag來存儲二級模型信息,如客戶信息。

控制器代碼:

public ActionResult Index() 
{ 
    var products = cm.Products.ToList(); 
    ViewBag.Customer = ... // get customer 
    return View(products); 
} 

查看代碼:

@model IEnumerable<WebApplication11.Models.ProductModel> 
@{ 
    ViewBag.Title = "Index"; 
    Layout = "~/Views/Shared/MyTemplate.cshtml"; 
    var customer = (CustomerModel)ViewBag.Customer; 
} 

@* display customer info *@ 
@* either { *@ 
    <div>Welcome @customer.CustomerFullName. You email is @customer.CustomerEmail</div> 
@* } or { *@ 
    @Html.Partial("_CustomerPartial", customer) 
@* } *@ 

@foreach(var product in Model) { 
    @* display product info *@ 
} 
  • 上一頁選項可以在一種方式爲使用強類型模型來改變。它也可以被稱爲查看模型 - 通常是一個只存儲視圖必要信息的類。在你的情況下,這些是一些客戶和產品信息。
  • 視圖模型:

    public class ProductListViewModel { 
        public CustomerModel Customer { get; set; } 
        public IEnumerable<ProductModel> Products { get; set; } 
    } 
    
    相關問題