views:

31

answers:

2

Hi All,

How do I conditionally add the style 'color:red' to the .CurrentDifference if the value happens to be a negative value?

Thanks, rod.

<div class="sRow">
    <div class="sLabel p40">
        Difference:
    </div>
    <%= (!String.IsNullOrEmpty(Model.Amount)?Model.Amount.CurrentDifference.ToString("c"):string.Empty) %>
</div>
+1  A: 

I would add a class to your style sheet for the red color. Then conditionally apply the class to a span as such.

<div class="sRow">
    <div class="sLabel p40">
        Difference:
    </div>
    <span class='<%= (Model.Amount>0?"Currency":"CurrencyRed") %>' >
    <%= (!String.IsNullOrEmpty(Model.Amount)?Model.Amount.CurrentDifference.ToString("c"):string.Empty) %>
    </span>
</div>
Jonathan Park
+1  A: 
<div class="sRow"> 
    <div class="sLabel p40"> 
        Difference: 
    </div> 
    <span style="<%=Model.Amount.CurrentDifference < 0  ? "color: #FF0000": ""%>"> 
      <%= (!String.IsNullOrEmpty(Model.Amount)?Model.Amount.CurrentDifference.ToString("c"):string.Empty) %> 
    </span>
</div> 

Note this is very sloppy. I would consider putting the logic for this in your Controller Action instead of putting conditional logic in your View. Perhaps use tempdata or even expose a new field in your model.

John Hartsock
@J.H. - for both the color and string.empty?
rod
im assuming you referring to my comment below my answer. Yes I would try to remove the logic from the view and apply that logic in the Controller action in some way. This way your view is a little cleaner and your debugging process can be followed easier in your Controller Action than <%=%> tags in your view.
John Hartsock
@J.H. - Yes, I was referring to your comment a moment ago. Thank you for the insight, rod.
rod