asp.net-mvc-3razor

got an error while using @if in razor


I've got an error when I tried to use if clause in razor like this:

Unexpected "if" keyword after "@" character.

@foreach (var item in Model) {
@if (item.Country != "No Country")
{
    <li>@Html.ActionLink(item.CountryWithCount, "IndexByProv", "EventInfo", new { country = item.Country }, null)</li>

Why I got this error?


Solution

  • You don't need @ because you are already inside a code block.

    @foreach (var item in Model) {
      if (item.Country != "No Country")
      {
        <li>something</li>
      }
    }