MVC Razor,将 if 语句添加到 foreach 循环 [英] MVC Razor, add if statement to foreach loop

查看:22
本文介绍了MVC Razor,将 if 语句添加到 foreach 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用剃刀将数据从我的模型添加到表中.我的问题是我想要一个 if 语句来决定标签应该是什么类,但我无法让它工作.

I'm trying to add data from my model to a table with razor. My problem is that i want an if statement to decide what class the tagg should be and i can't get this to work.

当我添加时,如果我运行代码时出现以下错误

When i add the if i get the following error when i run the code

The foreach block is missing a closing "}" character

我应该如何添加 if 语句?这是我当前的代码

How should i add the if statement? This is my current code

@{
      var counter = 0;            
}
@foreach (var item in Model)
{
       if(item.status == "Active") {
           <tr>
       }
       else {
           <tr class="danger">
       }
       <td>@counter</td>
       <td>@item.FirstName @item.LastName</td>
       <td>@item.Email</td>
       <td>@item.PhoneNumber</td>
       <td>Ändra</td>
       <td>Inaktivera</td>
        </tr>     
counter++;
}

推荐答案

MVC 应该检测 html 标签并将它们渲染出来,但是这似乎并不总是有效.

MVC should detect html tags and render those out, however it seem this doesnt always work.

在大括号之间,尝试添加一个标签

In between the curly brackets, try adding a tag

例如:

{
<text> 
   your html 
</text>
} 

如果您只是添加类,请尝试以下操作:

if you just adding the class try something like:

<tr @(item.status == "Active" ? String.Empty : "class="danger"" )>

这篇关于MVC Razor,将 if 语句添加到 foreach 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆