I'm trying to do a simple If/Else within a foreach with this code:
@{
var count = 0;
foreach (var item in Model)
{
if (count++ % 2 == 0)
{
@:<tr class="alt-row">
} else {
@:<tr>
}
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.Truncate(item.Details, 75)
</td>
<td>
<img src="@Url.Content("~/Content/Images/Projects/")@item.Images.Where(i => i.IsMain == true).Select(i => i.Name).Single()"
alt="@item.Images.Where(i => i.IsMain == true).Select(i => i.AltText).Single()" class="thumb" />
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ProjectId }) |
@Html.ActionLink("Details", "Details", new { id = item.ProjectId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ProjectId })
</td>
</tr>
}
}
I get a parse error "Encountered end tag "tr" with no matching start tag. Are your start/end tags properly balanced?". Seems like the if statement doesn't wanna' work.
Just use this for the closing tag:
@:</tr>
And leave your if/else as is.
Seems like the if statement doesn't wanna' work.
It works fine. You're working in 2 language-spaces here, it seems only proper not to split open/close sandwiches over the border.
I would just go with
<tr @(if (count++ % 2 == 0){<text>class="alt-row"</text>})>
Or even better
<tr class="alt-row@(count++ % 2)">
this will give you lines like
<tr class="alt-row0">
<tr class="alt-row1">
<tr class="alt-row0">
<tr class="alt-row1">
A little bit off topic maybe, but for modern browsers (IE9 and newer) you can use the css odd/even selectors to achieve want you want.
tr:nth-child(even) { /* your alt-row stuff */}
tr:nth-child(odd) { /* the other rows */ }
or
tr { /* all table rows */ }
tr:nth-child(even) { /* your alt-row stuff */}
To get rid of the if/else awkwardness you could use a using block:
@{
var count = 0;
foreach (var item in Model)
{
using(Html.TableRow(new { @class = (count++ % 2 == 0) ? "alt-row" : "" }))
{
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.Truncate(item.Details, 75)
</td>
<td>
<img src="@Url.Content("~/Content/Images/Projects/")@item.Images.Where(i => i.IsMain == true).Select(i => i.Name).Single()"
alt="@item.Images.Where(i => i.IsMain == true).Select(i => i.AltText).Single()" class="thumb" />
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ProjectId }) |
@Html.ActionLink("Details", "Details", new { id = item.ProjectId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ProjectId })
</td>
}
}
}
Reusable element that make it easier to add attributes:
//Block is take from http://www.codeducky.org/razor-trick-using-block/
public class TableRow : Block
{
private object _htmlAttributes;
private TagBuilder _tr;
public TableRow(HtmlHelper htmlHelper, object htmlAttributes) : base(htmlHelper)
{
_htmlAttributes = htmlAttributes;
}
public override void BeginBlock()
{
_tr = new TagBuilder("tr");
_tr.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(_htmlAttributes));
this.HtmlHelper.ViewContext.Writer.Write(_tr.ToString(TagRenderMode.StartTag));
}
protected override void EndBlock()
{
this.HtmlHelper.ViewContext.Writer.Write(_tr.ToString(TagRenderMode.EndTag));
}
}
Helper method to make razor syntax clearer:
public static TableRow TableRow(this HtmlHelper self, object htmlAttributes)
{
var tableRow = new TableRow(self, htmlAttributes);
tableRow.BeginBlock();
return tableRow;
}