简体   繁体   中英

razor syntax to write tags based on the condistion

@for (var i = 0; i < 3; i++)
{
  if(@Model.lstEmp[i].isTrue==true)
  {
    <tr bgcolor="red">
  }
  else
  {
   <tr>
  }
   <td>@Model.lstDept[i].DeptId</td>
   <td>@Model.lstEmp[i].EmpId</td>
</tr>
}

I am facing an error message that '}' not found. when I write everything inside the if and else block I'm not facing any issue but if I write like above I'm facing the issue. please help me in solving this.

Try the following:

@for (var i = 0; i < 3; i++)
{
    if(@Model.lstEmp[i].isTrue==true)
    {
        @:<tr bgcolor="red">
    }
    else
    {
        @:<tr>
    }
    @:<td>@Model.lstDept[i].DeptId</td>
    @:<td>@Model.lstEmp[i].EmpId</td>
    @:</tr>
}

I think the only problem is that you have if(@Model). You don't need the @ there because it is still in the razor context. Somehow it knows whether or not to interpret it as c# or html (not always perfect).

@for (var i = 0; i < 3; i++)
{
  if(Model.lstEmp[i].isTrue==true)
  {
    <tr bgcolor="red">
  }
  else
  {
   <tr>
  }
   <td>@Model.lstDept[i].DeptId</td>
   <td>@Model.lstEmp[i].EmpId</td>
</tr>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM