19

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

6 Answers 6

31

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\"" )>
Sign up to request clarification or add additional context in comments.

1 Comment

With my current version of MVC *3.2.3), I need to use this syntax <tr @(item.status == "Active" ? String.Empty : "class=danger" )> because the quotes get added automatically. But thanks for this tip!
6

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

Comments

2

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>

Comments

1

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

Comments

1

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>

Comments

0

You cannot use tag twice in a code block.

If you encounter problem because this limitation, put the second textbox as a string and then display it using html helper.

@{
    int loop=0;
    string HtmlBlock = "<table><tr><td style='font-weight:bold'>Lorem Text</td></tr></table>";
}
@foreach(var itemz in Mode.List){
    If(loop ==3){ Html.Raw(HtmlBlock ); } 
  
    <text>itemz.Name Itemz.NIP</text> 

    loop++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.