asp.net-mvc MVC Razor,将 if 语句添加到 foreach 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21301821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:28:25  来源:igfitidea点击:

MVC Razor, add if statement to foreach loop

asp.net-mvcrazorif-statementforeach

提问by Lord Vermillion

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.

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

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

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

The foreach block is missing a closing "}" character

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

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

@{
      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++;
}

回答by Mark Redman

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

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

In between the curly brackets, try adding a tag

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

eg:

例如:

{
<text> 
   your html 
</text>
} 

or

或者

if you just adding the class try something like:

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

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

回答by chamara

try below code.

试试下面的代码。

@{
    var counter = 0;            
}
@foreach (var item in Model)
{
       if(item.status == "Active") {
          <text> <tr> </text>
       }
       else {
           <text><tr class="danger"></text>
       }
       <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++;
}

回答by chamara

MVC detect HTML tags. So it will not add if statement like that. you can not use <text><text>also.

MVC 检测 HTML 标签。所以它不会添加这样的 if 语句。你也不能用<text><text>

You need to check condition in <tr>tag itself. See given result below.

您需要检查<tr>标签本身的条件。见下面给出的结果。

                          @{
                            var counter = 0;
                            }
                            <table>
                                @foreach (var item in Model)
                                {
                                <tr @(item.status=="Active" ? String.Empty : "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++;
                                }
                            </table>

回答by Abdul Hadi

You can add extension method that take bool or string depend on your needs

您可以根据需要添加采用 bool 或 string 的扩展方法

public static class HtmlHelpExtention
        {  
          public static string IsChecked(this IHtmlHelper htmlHelper,bool IsCheck, string className)
            {
                return IsCheck? className:"";
            }
         }

and then use it in the view

然后在视图中使用它

<tr class="@Html.IsChecked(item.IsGift,"teal accent-3")">

using this method will give you the ability to use multiple classes

使用此方法将使您能够使用多个类

回答by user3057544

Love Pandey solution works for me, but only for one class name. For more than one class name browser interpret second name as separate attribute. My modification for it is as below:

Love Pandey 解决方案适用于我,但仅适用于一个班级名称。对于多个类名,浏览器将第二个名称解释为单独的属性。我对它的修改如下:

    @{
        var counter = 0;
    }
    <table>
        @foreach (var item in Model)
            string className = item.status=="Active" ? String.Empty : "item second-class-name";
            {
                <tr class="@className">
                <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++;
             }
         </table>