开发者

ASP.NET MVC 2 C#: Short hand IF within loop?

开发者 https://www.devze.com 2023-01-29 03:35 出处:网络
I\'m trying to find out how I can put a condition within my loop as I print out list items... <ul>

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


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


Just return the needed string:

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

精彩评论

暂无评论...
验证码 换一张
取 消