Error MVC3 to MVC4 RTM manually. Update error before keyword keyword error:

I have an MVC3 project that has the following line of code that works fine:

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

Our team has a coding guide that should have the prefix of all local calls with this . This has worked fine in MVC3 so far.

I manually upgraded the project to MVC4 using the manual from here . Now the code is above the error with the following message:

 Unexpected "this" keyword after "@" character. Once inside code, you do not need to prefix constructs like "this" with "@". 

I think the error message is misleading. This means that it is legal, but not necessary. I intentionally do this, although I know that there is no need to adhere to coding rules. The fact that the parser fails should indicate the message Once inside code, you cannot prefix.... I understand that you cannot use @ in nested blocks of code, and the problem is not with the @ sign, but with the use of this . Adding this to the statement does not affect the result of the call, so I donโ€™t understand why it throws an exception. I can fix the problem by removing this :

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

But this will be contrary to our coding rules. So my question is that this was specifically changed in MVC4 (since it worked perfectly in MVC3). Or is this a bug in MVC4? If I remove the if block, I can still use the this .

 @this.Html.ActionLink("Add", "Add") 
+4
source share
1 answer

This is actually a bug in MVC4. A report has been sent .

Two workflows:

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

OR

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

Source: https://habr.com/ru/post/1434199/


All Articles