Reputation: 19128
im trying to write out divs every fourth row but im in some kinda zombie mode today that i cant figure it out. it looks like this
<% var count = 0; var totalCount = 0; foreach (var item in Model)
{
count++; totalCount = Model.Count(); %>
<% if (count % 5 == 0 || count == 1) {%>
<div class="row-me">
<%} %>
<div>
<h2>
<%= item.Name %>
</h2>
</div>
<% if (count % 4 == 0 || count == totalCount)
{%>
</div>
<%} %>
<% } %>
i want it to be
<div class="row-me">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
Upvotes: 0
Views: 264
Reputation: 21
Not an ASP programmer, but the fact that you're incrementing count
at the top of the loop could be throwing your calculations off, especially since you're using modulo. count
is never zero in your loop. Try incrementing count at the last line in your loop and your modulos should make more sense.
Untested:
<% var count = 0; var totalCount = 0; foreach (var item in Model)
{
totalCount = Model.Count(); %>
<% if (count % 4 == 0) {%>
<div class="row-me">
<%} %>
<div>
<h2>
<%= item.Name %>
</h2>
</div>
<% if (count % 4 == 0 || count == totalCount)
{%>
</div>
<%} %>
<% count++;
} %>
This should look like:
<div class="row-me"><div><h2>...</h2></div></div> <!-- count = 0 -->
<div><h2>...</h2></div> <!-- count = 1 -->
<div><h2>...</h2></div> <!-- count = 2 -->
<div><h2>...</h2></div> <!-- count = 3 -->
<div class="row-me"><div><h2>...</h2></div></div> <!-- count = 4 -->
<div><h2>...</h2></div> <!-- count = 5 -->
<div><h2>...</h2></div> <!-- count = 6 -->
<div><h2>...</h2></div> <!-- count = 7 -->
<div class="row-me"><div><h2>...</h2></div></div> <!-- count = 8 -->
Where ...
is the output of item.Name
Upvotes: 0
Reputation: 24212
The condition you want is this:
if ((count - 1) % 4 == 0)
Examples:
(1 - 1) % 4 = 0
(2 - 1) % 4 = 1
(3 - 1) % 4 = 2
(4 - 1) % 4 = 3
(5 - 1) % 4 = 0
(6 - 1) % 4 = 1
(7 - 1) % 4 = 2
(8 - 1) % 4 = 3
Upvotes: 1