我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>
爲了理解的目的我刪除的視圖某些部分。 請指導我在特定的視圖中訪問兩個模型。 我也嘗試了一些方法,但無法得到它。 在此先感謝...
Thnks!請給我發送視圖編碼,使用'@foreach loop()'訪問兩個模型 –
@foreach(模型中的var項) –
foreach(Model.Products中的var項) –