是否可以在一个视图中放置2个模型?我的页面是工作的罚款有一个模型,但是当我尝试添加其他模型线以下我的代码决定开始有一些错误:在1视图中有2个模型的方法,可以吗?
@model ProjectShop.Models.Delivery
我的代码之前,我添加上面一行的作品显示如下:
@model ProjectShop.Models.Products
@{
ViewBag.Title = "Products";
Layout = "~/Views/Shared/_ProductLayout.cshtml";
}
<p>@Html.DisplayFor(model => model.productname) </p>
在一个视图中有两个模型使我可以从两个来源调用数据的方法是什么?
编辑:
这是一类新的将作为HTML页面视图模型,但我认为它不是正确的在所有:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ProjectShop.Models;
namespace ProjectShop.ViewModels
{
public class ProductDelivTimes
{
public Models.Products;
public Models.Delivery;
}
}
编辑:
public class ProductDelivTimes
{
public Models.Products ProductModel;
public Models.Delivery DeliveryModel;
}
在我的HTML文件中:
@model IEnumerable<ProjectShop.ViewModels.ProductDelivTimes>
编辑:
@model IEnumerable<ProjectShop.ViewModels.PerformanceTimes>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
var grid = new WebGrid(Model, rowsPerPage: 5, defaultSort: "Name");
ViewBag.Created = TempData["ProductCreated"];
ViewBag.Created = TempData["ProductDeleted"];
}
@grid.GetHtml(
tableStyle: "grid",
headerStyle: "gridhead",
rowStyle: "gridRow",
alternatingRowStyle: "gridRow",
mode: WebGridPagerModes.All,
columns: grid.Columns(
grid.Column("Image", format: @<text><img src="@item.image" alt="" width="84px" height="128px"/></text>, style: "Image"),
grid.Column("Products", format: @<text>@Html.Raw(item.Products.name.Substring(0, item.Products.name.IndexOf(".") + 1))</text>, style: "Products")
))
数据将始终需要,其他所需的模型将显示页面上每个产品的交付日期。 – Blob 2012-03-27 15:25:43
是的,所以只需将它们组合成一个“包装”模型,知道如何组合来自两种不同模型的数据以供显示。 – Attila 2012-03-27 15:33:54