我的 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/