Simple If/Else Razor Syntax
我正在尝试使用以下代码在foreach中执行简单的If / Else:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | @{ 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> } } |
我收到一个解析错误"遇到不匹配的开始标签的结束标签" tr"。您的开始/结束标签是否正确平衡?"。 似乎if语句不起作用。
只需将其用作结束标记:
1 | @:</tr> |
并保持原样。
Seems like the if statement doesn't wanna' work.
它工作正常。 您在这里使用2种语言空间工作,似乎最好不要在边界上分开打开/关闭三明治。
我只是去
1 | <tr @(if (count++ % 2 == 0){<text>class="alt-row"</text>})> |
甚至更好
1 | <tr class="alt-row@(count++ % 2)"> |
这会给你线条
1 2 3 4 | <tr class="alt-row0"> <tr class="alt-row1"> <tr class="alt-row0"> <tr class="alt-row1"> |
也许有些话题,但是对于现代浏览器(IE9和更高版本),您可以使用css奇/偶选择器来实现所需的功能。
1 2 | tr:nth-child(even) { /* your alt-row stuff */} tr:nth-child(odd) { /* the other rows */ } |
要么
1 2 | tr { /* all table rows */ } tr:nth-child(even) { /* your alt-row stuff */} |
为了摆脱if / else的尴尬,您可以使用using块:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | @{ 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> } } } |
可重用的元素,使添加属性更加容易:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | //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方法:
1 2 3 4 5 6 | public static TableRow TableRow(this HtmlHelper self, object htmlAttributes) { var tableRow = new TableRow(self, htmlAttributes); tableRow.BeginBlock(); return tableRow; } |