【问题标题】:"@:" is not recognized“@:”无法识别
【发布时间】:2018-10-11 13:50:51
【问题描述】:

我正在使用 MVC5 和 Visual Studio 2017 社区,我在我的 Razor cshtml 视图页面中尝试使用 @:,但它无法识别它。

我收到此错误:

解析器错误

描述:解析服务此请求所需的资源时出错。请查看以下特定的解析错误详细信息并适当修改您的源文件。

解析器错误消息:“:”在代码块的开头无效。只有标识符、关键字、cmets、"(" 和 "{" 是有效的。**

<td>
    @:test
    @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
    @Html.ActionLink("Details", "Details", new { id=item.Id }) |
    @Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>

【问题讨论】:

  • 当您已经在代码块 (@{ ... }) 中时,没有理由使用 @:。为什么不简单地test

标签: c# .net razor asp.net-mvc-5


【解决方案1】:

您收到错误是因为您不在代码块中,而是在 HTML 中键入它。

@: 用于在 Razor 代码块中包含内容行。示例:

@if(name == "Mark") {
    @: Hello Mark
}

如果您有一个变量 test 并且您尝试在 HTML 中将其打印出来,那么请像这样使用 @

<td>
    @test
    @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
    @Html.ActionLink("Details", "Details", new { id=item.Id }) |
    @Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 2013-04-19
    • 2019-01-28
    • 2017-08-10
    • 2019-11-01
    • 2015-08-14
    相关资源
    最近更新 更多