MVC Razor, add if statement to foreach loop

…衆ロ難τιáo~ 提交于 2020-05-07 18:25:12

问题


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

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

回答1:


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

eg:

{
<text> 
   your html 
</text>
} 

or

if you just adding the class try something like:

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



回答2:


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



回答3:


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

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

                          @{
                            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>



回答4:


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

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




回答5:


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:

    @{
        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>


来源:https://stackoverflow.com/questions/21301821/mvc-razor-add-if-statement-to-foreach-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!