简体   繁体   English

在使用实体框架的MVC4 Intranet应用程序中无法在Razor语法中使用HtmlHelper

[英]Unable to Use an HtmlHelper in Razor syntax in MVC4 Intranet App using Entity Framework

I am using C# MVC4, Razor syntax and Entity Framework for a simple intranet. 我正在为简单的Intranet使用C#MVC4,Razor语法和实体框架。 I used EF to create my model from an existing database table called PrinterMapping. 我使用EF从称为PrinterMapping的现有数据库表中创建模型。 After creating it looks like this: 创建后看起来像这样:

public partial class PrinterMapping
{
    public string MTPrinterID { get; set; }
    public string NTPrinterID { get; set; }
    public string Active { get; set; }
}

I extended the partial class so that it has a Property that is not in the database table but I want it there anyway. 我扩展了部分类,以便它具有不在数据库表中的属性,但是无论如何我都希望它存在。

public partial class PrinterMapping
{
    public string ExceptionMessage { get; set; }
}

In my HomeController's Create action, I set the ExceptionMessage Property based on catching any Exception messages returned from saving to the database. 在HomeController的Create操作中,我基于捕获从保存到数据库中返回的所有Exception消息来设置ExceptionMessage属性。 I want to display this Property on my Index.chtml view. 我想在Index.chtml视图上显示此属性。

I am having trouble properly understanding strongly typed Html helpers that use Link. 我无法正确理解使用Link的强类型HTML助手。 So I have: 所以我有:

@Html.DisplayTextFor(model => model.ExceptionMessage)

I get the following error while trying to run the app: 尝试运行应用程序时出现以下错误:

Compiler Error Message: 编译器错误信息:

CS1061:
'System.Collections.Generic.IEnumerable<AccessPrinterMapping.PrinterMapping>'
does not contain a definition for 'ExceptionMessage' and no extension method 
'ExceptionMessage' accepting a first argument of type 
'System.Collections.Generic.IEnumerable<AccessPrinterMapping.PrinterMapping>'
could be found (are you missing a using directive or an assembly reference?)

Eh? 嗯? EF earlier on created the following code for me and there are no problems with the following code: EF较早时为我创建了以下代码,并且以下代码没有问题:

<th align="left">
    @Html.DisplayNameFor(model => model.MTPrinterID)
</th>
<th align="left">
    @Html.DisplayNameFor(model => model.NTPrinterID)
</th>
<th align="left">
    @Html.DisplayNameFor(model => model.Active)
</th>

The only difference here is that MTPrinter, NTPrinter and Active Properties were created by EF and I created the ExceptionMessage Property myself. 唯一的区别是MTPrinter,NTPrinter和Active Properties是由EF创建的,而我自己创建了ExceptionMessage属性。

Will some kind soul please explain a little bit of what is going on? 请问有什么好心的人可以解释一下发生了什么吗? Many thanks. 非常感谢。

What is happening is that you have extended the PrinterMapping class definition and add a new property which is not defined either in the EF Model and not column exist in the database. 发生的情况是,您扩展了PrinterMapping类定义并添加了一个新属性,该属性在EF模型中也未定义,并且数据库中不存在该列。

When you return the list of PrinterMapping the view try to fetch all the data from the database, but it fails beacuse it's unable to map the ExceptionMessage property. 当您返回PrinterMapping的列表时,请尝试从数据库中获取所有数据,但是由于无法映射ExceptionMessage属性,因此该视图失败。

You can use the Bind attribute (see Doc here ), to either exclude or include your property in the object binding process: 您可以使用Bind属性(请参见此处的 Doc)在对象绑定过程中排除或包括您的属性:

public partial class PrinterMapping
{
    [Bind(Exclude="ExceptionMessage")]
    public string ExceptionMessage { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM