1

I'm trying to find out how I can put a condition within my loop as I print out list items...

<ul>
   <% foreach (var filter in Model.Filter)
     { %>

     <li <% filter.TagChecked == 1 ? //yes : //no %>><%: filter.TagLabel %> <span class="closeImage"><img src="/Content/Images/filterButtonClose.gif" /></span></li>

   <% } %>
</ul>

I'm trying to see if a filter is checked... if yes, then I need to write the class and also add the image. If not, then just write a normal li

2 Answers 2

3
<ul>
<% foreach (var filter in Model.Filter)
 { %>

 <li <%: filter.TagChecked == 1 ? "class=\"some-class\"" : "" %>><%: filter.TagLabel %> <span class="closeImage"><img src="/Content/Images/filterButtonClose.gif" /></span></li>

<% } %>
</ul>
Sign up to request clarification or add additional context in comments.

1 Comment

With .NET 4.0, it is better practice to use <%:%> instead of <%=%>, as it adds HTML encoding.
2

Just return the needed string:

<%: filter.TagChecked == 1 ? " class=\"myclass\"" : string.Empty %>

2 Comments

Any reason why you chose to use string.Empty as opposed to ""?
@dcolumbus - I just like the explicitness of it. It evaluates to the same thing.

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.