在razor中使用@if时出错

San*_*San 2 razor asp.net-mvc-3

当我尝试在剃刀中使用if子句时,我遇到了错误,如下所示:

"@"字符后面的意外"if"关键字.

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

为什么我收到此错误?

Shy*_*yju 5

你不需要@ 因为你已经在代码块中了.

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