c# - CSHTML 文件中 lambda 表达式的运行时编译器问题

标签 c# visual-studio-2012 razor asp.net-mvc-5

我的 C# ASP.NET MVC5 项目中有两个 cshtml View 。这些 View 处理用户资料和公司资料。

我正在为这些字段制作编辑器,以便用户可以填写他们的个人资料。

在用户配置文件 View 中,编辑器中的 lambda 表达式无法在浏览器中编译。 用户 View 的相关部分:

@model Project.Models.UserProfileViewModel

@{
    ViewBag.Title = "User Profile";
}
<h2>User Profile</h2>

@using (Html.BeginForm( "UserProfile", "Profile", FormMethod.Post, new { enctype = "multipart/form-data" } ))
{
    <div>
        @Html.LabelFor( model -> model.Salutation )
        @Html.EditorFor( model -> model.Salutation )
    </div>
}

以及公司 View 的匹配部分:

@model Project.Models.CompanyProfileViewModel

@{
    ViewBag.Title = "Step 2:  Company Profile";
}
<h2>Company Profile</h2>

@using (Html.BeginForm( "CompanyProfile", "Profile", FormMethod.Post, new { enctype = "multipart/form-data" } ))
{
    <div>
        @Html.LabelFor(model => model.CompanyName)
        @Html.EditorFor(model => model.CompanyName)
    </div>
}

用户查看错误是:

Server Error in '/' Application.

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0103: The name 'model' does not exist in the current context

公司 View 工作得很好。对于它的值(value),当我使用 @Html.EditorForModel() 时,用户 View 工作得很好。

我没有看到我的 View 之间有任何差异,那么为什么 lambda 表达式仅在一个 View 中无法通过 JIT 编译?

作为附带问题,Razor v3 没有突出显示我的观点,也没有提供 Intellisense。但这是另一篇文章的问题。

最佳答案

-> 不等于 =>。

现在看到了吗? :)

应该是:

@Html.LabelFor( model => model.Salutation )
@Html.EditorFor( model => model.Salutation )

关于c# - CSHTML 文件中 lambda 表达式的运行时编译器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19981345/

相关文章:

c# - WCF REST Web 服务和 HTTP header

.net - 我无法再在 VS 2012 中调试运行 .NET 程序集的 native 代码

c# - MySQL Split 在 "SELECT WHERE IN"语句中使用

c# - 如何为 Devexpress XtraGrid 控件中的特定列进行数据绑定(bind)?

sql-server - SQL Server 2012 默认实例的连接字符串是什么?

msbuild - VS2012 中的 BeforeBuild 和其他目标发生了什么?

使用 Razor 语法的 Javascript 命名空间?

c# - Blazor 数据列表 onchange 事件未触发

javascript - 使用 Ajax.BeginForm 更新表单

c# - 在 VS 2017 中更改整个项目的命名空间