[英]Using Multiple model in a single view ASP.NET, MVC [duplicate]
这个问题已经在这里有了答案:
我正在淘汰像OLX这样的二手商品,我有两种型号
1:产品模型
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>
为了理解,我删除了部分视图。 请引导我以特定视图访问这两种模型。 我也尝试了一些方法,但无法获得。 提前致谢...
有多个选项可用于访问特定视图中的多个模型。 我假设您只想在页面角落或某些地方显示一些客户信息。 您的布局视图可能需要包含此客户信息(但没关系)。
创建CustomerController
public class CustomerController : Controller {
[ChildActionOnly]
public ViewResult CustomerInfo() {
CustomerModel customer = ...
return PartialView("_CustomerPartial", customer);
}
}
.chstml视图将具有@Html.Action("CustomerInfo", "Customer")
,您要在其中显示客户信息。
这样,您将有一个单独的CustomerModel
创建逻辑。
ViewData
或ViewBag
存储辅助模型信息,例如客户信息。 控制器代码:
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 *@
}
ViewModel:
public class ProductListViewModel {
public CustomerModel Customer { get; set; }
public IEnumerable<ProductModel> Products { get; set; }
}
您可以使用以下代码创建具有2个属性的视图模型
public class MyModel
{
public List<ProductModel> Products { get; set; }
public List<CustomerModel> Customers { get; set; }
}
在控制器中:
public ActionResult Index()
{
var model = new MyModel
{
Products = cm.Products.ToList(),
Customers = cm.Customers.ToList()
};
return View(model);
}
在Veiw
@foreach (var item in Model.Products)
{
@*Write your razor code here*@
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.