2017-01-01 70 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; } 
    } 
    
    相关问题