小编use*_*439的帖子

在Razor-4视图中访问模型属性

我有以下EF生成的数据模型:

public partial class PrinterMapping
{
    public string MTPrinterID { get; set; }
    public string NTPrinterID { get; set; }
    public string Active { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后我有以下视图模型:

public class PrinterViewModel
{
    public PrinterMapping PrinterMapping;
    public Exceptions Exceptions;
    public IEnumerable<PrinterMapping> Printers;
}
Run Code Online (Sandbox Code Playgroud)

在HomeController中的Index Action中,我将视图模型传递给Index视图.

private eFormsEntities db = new eFormsEntities();
public ActionResult Index()
{
    PrinterViewModel printerModel = new PrinterViewModel();
    printerModel.Printers = from pt in db.PrinterMapping select pt;

    return View(printerModel);
}
Run Code Online (Sandbox Code Playgroud)

我的索引视图以下面的方式调用部分视图(可能是错误的):

@Html.Partial("~/Views/Home/GridView.cshtml")
Run Code Online (Sandbox Code Playgroud)

我的GridView.cshtml看起来像:

@model AccessPrinterMapping.Models.PrinterViewModel

<h2> This is Where …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc viewmodel razor asp.net-mvc-4

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

asp.net-mvc ×1

asp.net-mvc-4 ×1

c# ×1

razor ×1

viewmodel ×1