我有以下Razor代码,它是静态的,需要从Model:
构造<div class="row">
<div class="col-lg-4 col-md-3 hidden-sm">
<h4>@Resource.Title</h4>
<p>@Resource.Header_Date</p>
<img src="~/Content/Images/student.png" alt="student" />
</div>
<div class="col-lg-8 col-md-9">
<h4>@Resource.Header_ListTitle</h4>
<div class="row">
<div class="col-sm-6">
<div class="divider"></div>
<ul class="menu">
<li>@Html.ActionLink(Resource.Faculty_One, "Faculty", "Home", new { id = 1 }, null)</li>
<li>@Html.ActionLink(Resource.Faculty_Three, "Faculty", "Home", new { id = 3 }, null)</li>
<li>@Html.ActionLink(Resource.Faculty_Five, "Faculty", "Home", new { id = 5 }, null)</li>
</ul>
</div>
<div class="col-sm-6">
<div class="divider"></div>
<ul class="menu">
<li>@Html.ActionLink(Resource.Faculty_Two, "Faculty", "Home", new { id = 2 }, null)</li>
<li>@Html.ActionLink(Resource.Faculty_Four, "Faculty", "Home", new { id = 4 }, null)</li>
<li>@Html.ActionLink(Resource.Faculty_Six, "Faculty", "Home", new { id = 6 }, null)</li>
</ul>
</div>
</div>
</div>
</div>
我正在尝试做同样但不太确定要打开哪个元素关闭哪个/何时例如
<div class="row">
<div class="col-lg-4 col-md-3 hidden-sm">
<h4>@Resource.Title</h4>
<p>@Resource.Header_Date</p>
<img src="~/Content/Images/student.png" alt="student" />
</div>
<div class="col-lg-8 col-md-9">
<h4>@Resource.Header_ListTitle</h4>
<div class="row">
@foreach (var item in Model)
{
if (counter != 0 && counter % 2 == 0)
{
@:</li>
@:<li>
}
<div class="col-sm-6">
<div class="divider"></div>
@* what to do here? *@
</div>
counter++;
}
</div><!-- /end row -->
这完全令人困惑。提前谢谢你的帮助。
答案 0 :(得分:0)
您可以使用三元运算符:
<div class="divider"></div>
<ul class="menu">
<li>@Html.ActionLink(counter % 2 == 0 ?Resource.Faculty_Two : Resource.Faculty_One, "Faculty", "Home", new { id = counter % 2 == 0 ? 2 : 1 }, null)</li>
<li>@Html.ActionLink(counter % 2 == 0 ?Resource.Faculty_Four : Resource.Faculty_Three, "Faculty", "Home", new { id = counter % 2 == 0 ? 4 : 3 }, null)</li>
<li>@Html.ActionLink(counter % 2 == 0 ?Resource.Faculty_Six : Resource.Faculty_Five, "Faculty", "Home", new { id = counter % 2 == 0 ? 6 : 5 }, null)</li>
</ul>
</div>
答案 1 :(得分:0)
这就是我的结尾:
<div class="row">
<div class="col-lg-4 col-md-3 hidden-sm">
<h4>@Resource.Title</h4>
<p>@Resource.Header_Date</p>
<img src="~/Content/Images/student.png" alt="student" />
</div>
<div class="col-lg-8 col-md-9">
<h4>@Resource.Header_ListTitle</h4>
<div class="row">
<div class="col-sm-6">
<div class="divider"></div>
<ul class="menu">
@foreach (var item in Model.Where(e => e.ID % 2 == 0))
{
<li>@Html.ActionLink(item.FacultyName, "Faculty", "Home", new { id = item.ID }, null)</li>
}
</ul>
</div>
<div class="col-sm-6">
<div class="divider"></div>
<ul class="menu">
@foreach (var item in Model.Where(e => e.ID % 2 != 0))
{
<li>@Html.ActionLink(item.FacultyName, "Faculty", "Home", new { id = item.ID }, null)</li>
}
</ul>
</div>
</div>
</div>
</div>
实际上它比我想象的要简单得多。谢谢你们所有的投入。非常感谢
答案 2 :(得分:-1)
一般来说,你需要两个循环,一个用于奇数,一个用于偶数id。第一个循环可能如下所示:
<ul>
@foreach (var item in Model)
{
if (counter % 2 == 0)
{
<li>@Html.ActionLink(Resource.Faculty_Three, "Faculty", item.XXX, new { id = counter }, null)</li>
}
counter++;
}
</ul>
通常你想在Razor代码块(foreach
,if
等)中生成完整的HTML元素,所以请避免这样的事情:
if (foo) {
@: <li>
}
if (foo) {
@: </li>
}
不起作用,除了Razor默认转义<
和>
个字符外,您还需要使用@Html.Raw
将它们放入输出中。更好的方法是构建视图,以便每个元素都是整体生成的:
<li>foo</li>
。如果您想避免代码重复,可能需要查看Razor助手:http://weblogs.asp.net/scottgu/asp-net-mvc-3-and-the-helper-syntax-within-razor