gpt4 book ai didi

razor - MVC3 到 MVC4 RTM 手动升级问题 : @ in front of this keyword throwing error

转载 作者:行者123 更新时间:2023-12-01 01:15:15 26 4
gpt4 key购买 nike

我有一个 MVC3 项目,它具有以下代码行,效果很好:

@if (this.Model.ShowAddButton)
{
@this.Html.ActionLink("Add", "Add")
}

我们的团队有一个编码指南,所有本地方法调用都需要以 this 为前缀.到目前为止,这在 MVC3 中运行良好。

我已使用 here 中的指导手动将项目升级到 MVC4。 .现在上面的代码出现错误并显示以下消息:
Unexpected "this" keyword after "@" character.  Once inside code, you do not need to prefix constructs like "this" with "@".

我认为错误信息具有误导性。这意味着这样做是合法的,但不是必需的。即使我知道不需要遵守编码准则,我也是故意这样做的。解析器失败的事实,消息应指示 Once inside code, you cannot prefix....我知道你不能使用 @在嵌套代码块中,问题不在于 @签名,但使用 this .添加 this到语句不会影响调用的结果,所以我不明白为什么它会抛出异常。我可以通过删除 this 来解决此问题:
@if (this.Model.ShowAddButton)
{
@Html.ActionLink("Add", "Add")
}

但是这样做会与我们的编码指南相冲突。所以我的问题是,这是否是在 MVC4 中故意改变的(因为它在 MVC3 中工作得很好)。或者这是 MVC4 中的错误?如果我删除 if block ,我仍然可以使用 this关键词。
@this.Html.ActionLink("Add", "Add")

最佳答案

这实际上是一个 bug在 MVC4 中。一个 report被归档。

两种解决方法是:

@if (this.Model.ShowAddButton)
{
@Html.ActionLink("Add", "Add")
}

或者
@if (this.Model.ShowAddButton)
{
@(this.Html.ActionLink("Add", "Add"))
}

关于razor - MVC3 到 MVC4 RTM 手动升级问题 : @ in front of this keyword throwing error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12425604/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com