Blo*_*lob 0 c# asp.net-mvc razor
是否可以在一个视图中放置2个模型?我的页面可以正常使用一个模型,但是当我尝试在我的代码下面添加其他模型行时,决定开始出现一些错误:
@model ProjectShop.Models.Delivery
Run Code Online (Sandbox Code Playgroud)
我添加上面一行之前的代码如下所示:
@model ProjectShop.Models.Products
@{
ViewBag.Title = "Products";
Layout = "~/Views/Shared/_ProductLayout.cshtml";
}
<p>@Html.DisplayFor(model => model.productname) </p>
Run Code Online (Sandbox Code Playgroud)
在一个视图中使用2个模型的方法是什么,使我能够从两个来源调用数据?
编辑:
这是一个新类,它将充当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;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
public class ProductDelivTimes
{
public Models.Products ProductModel;
public Models.Delivery DeliveryModel;
}
Run Code Online (Sandbox Code Playgroud)
在我的html文件中:
@model IEnumerable<ProjectShop.ViewModels.ProductDelivTimes>
Run Code Online (Sandbox Code Playgroud)
编辑:
@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")
))
Run Code Online (Sandbox Code Playgroud)
如果要将多个Domain实体作为视图的一部分,请考虑创建一个组合两个数据源模型的View Model
像(只是伪代码)的东西:
ProductDeliveryViewModel
{
// ProductModel
// DeliveryModel
}
Run Code Online (Sandbox Code Playgroud)
所以你会的
@model ProjectShop.Models.ProductDeliveryViewModel
@Html.DisplayFor (model=> model.ProductModel.Property)
@Html.DisplayFor (model=> model.DeliveryModel.Property)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
258 次 |
| 最近记录: |