Saturday

MVC Razor View: Unexpected if keyword after @ character in MVC Razor View

Introduction

I am going to explain a very popular error i.e Unexpected "if" keyword after "@" character while working with ASP.Net MVC Razor View, I am sharing all aspects of this error Unexpected "if" keyword after "@" character.

Error Detail:

Unexpected "if" keyword after "@" character.  Once inside code, you do not need to prefix constructs like "if" with "@".
When Error "Unexpected "if" keyword after "@" character" comes off
Working on MVC project and working on MVC Razor view, meanwhile a problem faced me whenever using foreach statement in MVC Razor View Engine. I was using nested statement to write some logic. I have been shared my both preview of code 1) Wrong statement and 2) corrected statement which is showing below.

Unexpected if keyword after character in MVC Razor View

Wrong Statement

@foreach (var item in Model)
{
    @if (item.isCorrect)
    {
           // do something
    }
}

Correct Statement

@foreach (var item in Model)
{
    if (item.isCorrect)
    {
           // do something
    }
}

Conclusion 

Explained above, how can we use nested loop in Razor view, in this article, fully explained how can we remove issue of nested loop Unexpected if keyword after @ character.

Suggested Reading