如何使用 Razor 通过循环正确生成引导网格? [英] How properly generate bootstrap grid via loop using Razor?

查看:22
本文介绍了如何使用 Razor 通过循环正确生成引导网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ASP.NET MVC 和引导程序.我有很多对象 (>2) 在集合中,每个对象都需要一个 <div class="col-xs-6"> 但连续只有 2 个列.如何使用循环实现这一目标?有 1 种方法,但我正在寻找更好的方法:

@model List@using (Html.BeginForm("ActionName", "ControllerName")){<div class="row">@for (int i = 0; i < Model.Count; i++){如果 (我 % 2 != 0) {<div class="row"><div class="col-xs-6">@Html.TextBoxFor(o => o[i].Value)

} 别的 {<div class="col-xs-6">@Html.TextBoxFor(o => o[i].Value)

}}

}

解决方案

关闭行 div 并在每第二次迭代时在循环内开始一个新的行

@for (int i = 0; i < Model.Count; i++){如果 (i > 0 && i % 2 == 0){@:</div><div class="row">//关闭并开始新行}<div class="col-xs-6">@Html.TextBoxFor(o => o[i].Value)

}

I use ASP.NET MVC and bootstrap. I have many objects (>2) in collection and for each need a <div class="col-xs-6"> but with only 2 cols in a row. How to achive this using loop? There is 1 way but I am looking for something better:

@model List<Object>
@using (Html.BeginForm("ActionName", "ControllerName"))
{
    <div class="row">
    @for (int i = 0; i < Model.Count; i++)
    {
        if (i % 2 != 0) {
        <div class="row">
            <div class="col-xs-6">
                @Html.TextBoxFor(o => o[i].Value)
            </div>
        </div>
        } else {
            <div class="col-xs-6">
            @Html.TextBoxFor(o => o[i].Value)
            </div>
        }
    }
    </div>
}

解决方案

Close the row div and start a new one inside the loop on every 2nd iteration

<div class="row">
    @for (int i = 0; i < Model.Count; i++)
    {
        if (i > 0 && i % 2 == 0)
        {
            @:</div><div class="row"> // close and start new row
        }
        <div class="col-xs-6">
            @Html.TextBoxFor(o => o[i].Value)
        </div>
    }
</div>

这篇关于如何使用 Razor 通过循环正确生成引导网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆