在编辑器模板中呈现 2 列布局

Rendering 2-column layout in an Editor Template

我想要一个 2 列布局,但问题是我无法将一半的 HTML 代码放在 else 段中。有什么想法吗?

这是编辑器模板:

@{int i = 0;}
@foreach (var property in ViewData.ModelMetadata.Properties)
{
    if (property.PropertyName.StartsWith("Z") ||
                property.IsReadOnly)
    {
        continue;
    }
    if (i++ % 2 == 0)
    {
        <div class="form-group form-inline col-xs-12">
            <div class="col-xs-5">
                @Html.Label(property.DisplayName, new { @for = property.PropertyName, @class = "control-label col-xs-3" })
                @Html.TextBox(property.PropertyName, new { @class = "form-control" })
            </div>
            <div class="col-xs-5">
                @Html.Label(property.DisplayName, new { @for = property.PropertyName, @class = "control-label col-xs-3" })
                @Html.TextBox(property.PropertyName, new { @class = "form-control" })
            </div>
        </div>
    }
    else
    {

    }
}

第二个<div class="col-xs-5">和结束标签应该在else中。

不确定您在 else 中到底想要什么,但是您应该在遍历其他对象之前声明父对象 div。此外,如果它是两列,它们应该是 col-xs-6,如 bootstrap 中所述。

 @{int i = 0;}
<div class="form-group form-inline">
<div class="row">
    @foreach (var property in ViewData.ModelMetadata.Properties)
    {
        if (property.PropertyName.StartsWith("Z") ||
                    property.IsReadOnly)
        {
            continue;
        }
        if (i++ % 2 == 0)
            <div class="col-xs-6">
                    @Html.Label(property.DisplayName, new { @for = property.PropertyName, @class = "control-label col-xs-3" })
                    @Html.TextBox(property.PropertyName, new { @class = "form-control" })
                </div>
        }
        else
        {
            <div class="col-xs-6">
                  @Html.Label(property.DisplayName, new { @for = property.PropertyName, @class = "control-label col-xs-3" })
                  @Html.TextBox(property.PropertyName, new { @class = "form-control" })
            </div>
        }
    }
</div>
</div>

我认为您尝试呈现表单的方式太复杂了,col-xs-** 已经向左浮动,您不必担心 "odd left - even right"。

<div class="row form-horizontal">
@foreach (var property in ViewData.ModelMetadata.Properties)
{
    if (property.PropertyName.StartsWith("Z") || property.IsReadOnly)
    {
        continue;
    }
    <div class="col-xs-6">
        <div class="form-group">
             @Html.Label(property.DisplayName, new { @for = property.PropertyName, @class = "control-label col-xs-12 col-sm-3" })
             <div class="col-xs-12 col-sm-3">
                  @Html.TextBox(property.PropertyName, new { @class = "form-control" })
             </div>            
        </div>
    </div>
}
</div>

您需要启动一个包含 <div> 的块,然后在 if 块中关闭并启动一个包含 <div>

的新块
@{int i = 0;}
<div class="form-group form-inline col-xs-12">
    @foreach (var property in ViewData.ModelMetadata.Properties) {
        if (property.PropertyName.StartsWith("Z") || property.IsReadOnly) {
            continue;
        }
        <div class="col-xs-6">
            @Html.Label(property.DisplayName, new { @for = property.PropertyName, @class = "control-label col-xs-3" })
            @Html.TextBox(property.PropertyName, new { @class = "form-control" })
        </div>
        if (++i % 2 == 0) {
            @:</div><div class="form-group form-inline col-xs-12">
        }
    }
</div>