Razor V2 Bug: Not able to use @this inside code block


In Razor v2, we are not able to use @this inside razor code block. For example, if we use this in Razor V2 then the parser shows a parser error.
@if (this.Model.ShowAddButton)
 @this.Html.ActionLink("Add", "Add")

Detail can be found here.
Closed Apr 15, 2013 at 10:57 PM by eilonlipton
This indeed appears to be an odd bug. Fortunately there are at least a few easy workarounds:
  1. Wrap the markup in the special <text></text> tags, which causes anything within those tags to be considered markup.
  2. Prefix the one line of markup with the special "@:" prefix that says that everything after that prefix is considered markup.
  3. As one commenter suggested, wrap the code after the "@" in parentheses.
We realize this might be a breaking change from the previous version, but this type of code construct is not very common and changing the parser for something that has an easy workaround is too risky and would likely break other cases.


jesiuu wrote Jan 31, 2013 at 9:10 AM

A workaround can be putting a statement into a brackets:

@(this.Html.ActionLink("Add", "Add"))

But this is a bug in my opinion and should be repaired.

iamwill wrote Mar 6, 2013 at 6:11 PM

This is a big deal when upgrading projects from MVC 3 to 4.

franprdv wrote Oct 28, 2013 at 10:05 AM

[...] but this type of code construct is not very common [...]
Are you sure? what are your sources?