Unbalanced DIV Closing DIV with MVC Razor For Each Loop - asp.net-mvc

I'm using Bootstrap 3 on my site and I am using an MVC Razor For Each Loop to retrieve all of the child items on a blog homepage as follows:
#{int i = 0;}
#foreach (var subPage in Model.Content.AncestorOrSelf(2).Children.OrderBy("createDate descending"))
{
if (i % 2 == 0)
{
#:<div class="row row-eq-height">
}
<div class="col-sm-12 col-md-6">
</div><!-- col-sm-12 col-md-6 -->
if (i % 2 != 0)
{
#:</div><!-- row row-eq-height -->
}
i++;
}
This works fine when I have an even number of child pages however when I have an odd number of child pages, it's not hitting the if % 2 != 0 block which means the last row doesn't get closed which then breaks the flow of the remainder of the page.
I tried to fix this by adding the following if statement after the For Each loop hoping that if the final value of i was not 2, it would add a closing DIV tag as needed but instead that causes an inbalanced DIV tag error.
#if (i !% 2 == 0)
{
</div><!-- row row-eq-height -->
}
Anyone got any thoughts or ideas? If I have an even number, it works just fine and the page flow is as expected.

Your foreach loop needs to be inside a the containing element (row) and then withing the loop, close and start a new row for every second child element (column)
#{int i = 0;}
<div class="row row-eq-height">
#foreach (var subPage in Model.Content.AncestorOrSelf(2).Children.OrderBy("createDate descending"))
{
if (i > 0 && i % 2 == 0)
{
#:</div><div class="row row-eq-height">
}
<div class="col-sm-12 col-md-6">xxxxx</div>
i++;
}
</div>
The output, assuming you have 5 items in the collection, will be
<div class="row row-eq-height">
<div class="col-sm-12 col-md-6">xxxxx</div>
<div class="col-sm-12 col-md-6">xxxxx</div>
</div>
<div class="row row-eq-height">
<div class="col-sm-12 col-md-6">xxxxx</div>
<div class="col-sm-12 col-md-6">xxxxx</div>
</div>
<div class="row row-eq-height">
<div class="col-sm-12 col-md-6">xxxxx</div>
</div>

Related

display block not working Razor in asp.net mvc

I have 2 for loops in Razor syntax , I want to implement each one in a block , I added style block to second for loop but it appears inline with the first line of contents to the first loop
<div style="display:block;">
#foreach (var Item in Model)
{
<div class="col-sm-3">
<div class="content">
<p>#Item.Name</p>
</div>
</div>
}
<br>
</div>
// the page buttons appear next to #Item.Name (at the first line of the loop items) not after it
<div style="display:block;">
#for (int i = 1; i <= ViewBag.TotalPages; i++)
{
<button class="btn btn-info" type="button" id="addressSearch" onclick="location.href='#Url.Action("myaction","mycontroller", new { page = i } )'">#i</button>
}
</div>
this should work if it is not !important in external stylesheet

How to add div tag after fixed number of iteration in mvc

I am using Umbraco (mvc) with bootstrap.My template use partial view to load name and job title of staff in template. How I can add div (new row) after 4 iteration.
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#{
var selection = Model.Content.Site().FirstChild("aboutUs").Children("staff")
.Where(x => x.IsVisible());
}
#foreach(var item in selection){
<div class="row team">
<div class="col-sm-3">
<div class="thumbnail-style">
<h3><a>#item.GetPropertyValue("fullName")</a> <small>#item.GetPropertyValue("jobTitle")</small></h3>
</div>
</div>
</div>
}
Difficulty:
I want to add
<div class="row team">
</div>
after every 4th
<div class="col-sm-3">
</div>
A more easier solution would be the use of the Umbraco helper method InGroupsOf(columns).
#foreach(var group in selection.InGroupsOf(4))
{
<div class="row team">
#foreach(var item in group)
{
<div class="col-sm-3">
<div class="thumbnail-style">
<h3><a>#item.GetPropertyValue("fullName")</a> <small>#item.GetPropertyValue("jobTitle")</small></h3>
</div>
</div>
}
</div>
}
Try:
#foreach(var obj in selection.Select((item, index) => new {item, index}))
{
if(obj.index==0||obj.index%4==0){
#Html.Raw("<div class='row team'>")
}
<div class="col-sm-3">
<div class="thumbnail-style">
<h3><a>#obj.item.GetPropertyValue("fullName")</a> <small>
#obj.item.GetPropertyValue("jobTitle")</small></h3>
</div>
</div>
#if((obj.index+1)%4==0||obj.index+1==selection.Count()){
#Html.Raw("</div>")
}
}
- It will works, Use for loop instead of foreach loop and then just add logic
#for (int i = 1; i < selection.Count; i++)
{
var item = selection[i];
<div class="row team">
<div class="col-sm-3">
<div class="thumbnail-style">
#if (i % 5 == 0)
{
<div class="col-sm-3">
<h4 >D #item</h4> <!--your actual html code-->
</div>
}
else
{
<h4 style="color:red">D #item</h4> <!--your actual html code-->
}
</div>
</div>
</div>
}

How to show unsorted images in mvc5 with Razor engine?

I'm using mvc5 with razor view engine and I have a table of images in my DB , I want show 3 images in first line, then show some text in the next line, and then show 2 images in the next line with some text which will repeat by for loop like this image
my current code is :
<div style="padding: 0;">
#foreach (var item in Model.MainGoods)
{
<div class="col-sm-12 col-md-6 col-lg-4">
<a href="#">
<img src="#Url.Content(item.GoodImage.ToString())" />
</a>
</div>
}
</div>
Can anyone help me do it?
This should do the trick:
<div style="padding: 0;">
#{
var count =1;
<div class="row">
#foreach (var item in Model.MainGoods)
{
if (count <= 3)
{
<div class="col-md-4 well">
<a href="#">
<img src="#Url.Content(item.GoodImage.ToString())"/>
</a>
</div>
if (count == 3)
{
//new row
#Html.Raw("</div><div class=\"row\">")
}
}
else
{
<div class="col-md-6 well">
<a href="#">
<img src="#"/>
</a>
</div>
if (count == 5)
{
//new row
#Html.Raw("</div><div class=\"row\">")
}
}
if (count < 5)
{
//increment the count
count++;
}
else
{
//reset the count
count = 1;
}
}
</div>
}
</div>

How do I disable Razor being clever with opening and closing tags?

In a loop I need to wrap divs after first one into mechanism that hides them and shows when expanded, easy right.
//above this lives code that draws a product section
if (product.DisplayOrder == 1 && Model.Count > 1)
{
<div class="arrow-with-text" style="cursor: pointer;">
<div class="arrow-right"></div>
<div class="arrow-down" style="display: none"></div>
<div style="margin-left: 2em;margin-top: -1.3em;font-size: 1.2em;color: #2280c4;">Other available Wills…</div>
</div>
<div class="other-wills" style="display: none">
}
else (product.DisplayOrder == Model.Count)
{
</div>
}
What I expected was all sections that go after first being wrapped in other-wills div and then being closed on last one. What it actually get's rendered to is this.
<div class="arrow-with-text" style="cursor: pointer;">
<div class="arrow-right"></div>
<div class="arrow-down" style="display: none"></div>
<div style="margin-left: 2em;margin-top: -1.3em;font-size: 1.2em;color: #2280c4;">Other available Wills…</div>
</div>
<div class="other-wills" style="display: none">
}
else (product.DisplayOrder == Model.Count)
{
</div>
<div class="sections-wrapper">
which isn't exactly what I expected (it reendered }else product.DisplayOrder == Model.Count){ as text).
How do I make razor interpret my closing of if statement and else correctly?
Use
#Html.Raw("</div>")
and
#Html.Raw("<div class=\"your-class\">")
for the 'stray' dynamic divs

(MVC 3 Razor) - Easier way to loop through 3 column div

At the moment I'm using the following code in my CSHTML:
#{int i = 0;}
#foreach (var item in Model.Traders)
{
if ((i++ % 3) == 0) {
if (i != 1) {
#:</div>
}
#:<div class="row">
}
#:<div class="four column"><div class="panel new"><h3 class="dotted"><strong>#item.Title</strong></h3><p>#item.Description</p><code><div class="panel pick"></code></div></div>
}
#if (i != 0) {
#:</div>
}
This outputs the following HTML:
<div class="row">
<div class="four column"><div class="panel new"><h3 class="dotted"><strong>Title</strong></h3><p>Description</p><code>code</code></div></div>
<div class="four column"><div class="panel new"><h3 class="dotted"><strong>Title</strong></h3><p>Description</p><code>code</code></div></div>
<div class="four column"><div class="panel new"><h3 class="dotted"><strong>Title</strong></h3><p>Description</p><code>code</code></div></div>
</div>
<div class="row">
<div class="four column"><div class="panel new"><h3 class="dotted"><strong>Bobby</strong></h3><p>Bobby bobby bobby</p><code><div class="panel pick"></code></div></div>
<!-- Add missing divs if there's less than 3 (there always needs to be 3 divs inside a div row). In this case it's 2 that are missing -->
<div class="four column"></div> <!-- my code does not render these -->
<div class="four column"></div> <!-- my code does not render these -->
</div>
My question is whether there's an easier way to achieve what I'm doing within my view and to ensure that it adds the missing divs if there's less than 3 in a row.
Group traders in batch of 3 items, Try this:
#{
var g = Model.Traders.GroupBy(r => Model.Traders.IndexOf(r) / 3).ToList();
}
#foreach (var parentItem in g)
{
<div class="row">
#foreach (var item in parentItem)
{
<div class="four column"><div class="panel new"><h3 class="dotted"><strong>#item.Title</strong></h3><p>#item.Description</p><code><div class="panel pick"></code></div></div>
}
#for (int i = parentItem.Count(); i < 3; i++)
{
<div class="four column"></div>
}
</div>
}
Regards.

Resources