6

Depending on my record, I'd like to change the style of the table row in my current iteration.

The below example doesn't work, but how would I go about doing this correctly?

 foreach (var item in Model)
                {
                    @{ 
                        if (item.DataState != DataState.Active)
                        {
                            <tr style="background-color: red">
                        }
                        else
                            <tr>
                    }
                    <td>@item.Name</td>
                  </tr>
                 }

So effectively, I'd like to dynamically render the <tr> element differently based on the DataState of my model.

1
  • Since you are already in a code block (foreach) you don't need to add an other code block with @{}. There is an other issue in your code - Razor doesn't support IF and ELSE without brackets. You have to put the else part in brackets. Look at my answer - there's a shorter approach. Commented Aug 12, 2015 at 7:49

7 Answers 7

22

Here's a shorter approach:

@foreach (var item in Model)
{
    <tr @(item.DataState != DataState.Active ? "style=background-color:red" : "")>
        <td>@item.Name</td>
    </tr>
}
Sign up to request clarification or add additional context in comments.

4 Comments

It doesn't seem to work. The row renders as: <tr red;&#39;="" style="'color:">
I fixed it by removing all whitespace from the css properties and values. Strange. Its now: <tr @(item.DataState != DataState.Active ? "style=background-color:rgba(255,0,0,.1)" : "")> Could you please update your answer to note the whitespace? Then I'll mark it as the answer. Thanks a ton.
I've fixed the code. I would suggest you to use css classes instead of changing style like this.
You're right, but for the sake of the example you provided, others might try it verbatim and find it doesn't work off the bat :)
5

There are multiple way you can write condition.

Option 1:

@foreach (var item in Model)
{
    if (item.DataState != DataState.Active)
    {
        <tr style="background-color: red">
            <td>@item.Name</td>
        </tr>
    }
    else
    {
       <tr>
            <td>@item.Name</td>
       </tr>
    }
}

Option 2:

@foreach (var item in Model)
{
    <tr style="@( item.DataState != DataState.Active ? "background-color: red;" : "" )">
        <td>@item.Name</td>
    </tr>
}

1 Comment

Def the most thorough answer. #thumbsup @sender
1

Not sure what kind of error you faces right now, but i gess your problem is that Razor don't understand all cases of tricky html render.

You can force him to render html where you need with @: symbol for single string or <text> tag like this:

 @foreach (var item in Model)
 {
    if (item.DataState != DataState.Active)
    {
        @: <tr style="background-color: red">
    }
    else
    { 
        <text><tr></text>
    }
    <td>@item.Name</td>
    </tr>
 }

1 Comment

I know that's an old post but it was just exactly what I needed today. Thank you!
1

Define the attributes in a variable. The razor parser will omit the attribute if its value is null

@foreach (var item in Model)
{
  var attributes = item.DataState == DataState.Active ? null : "background-color: red";
  <tr style=@attributes>
    <td>@item.Name</td>
  </tr>
}

Comments

0

You're probably going to get compile errors if you start splitting the <tr> tag up in the view and I'd not duplicate large chunks of code. I would do the following:

@foreach(var item in Model) {
    string strStyle=String.Empty;
    if(item.DataState!=DataState.Active) { strStyle = "background-color:red;" }
   <text>
   <tr style="@strStyle">
       <td>@item.Name</td>
   </tr>
   </text>
}

Comments

0
 @(item.DataState != DataState.Active  ? "style=" + "background-color:red" : "")

Comments

0
@(!string.IsNullOrWhiteSpace(Model?.District?.Name) ? Html.Raw($"<span class=\"location\">{Model?.District?.Name}</span>") : string.Empty)

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.