【问题标题】:How do I change the MVC Bootstrap layout to have more than one columns如何将 MVC Bootstrap 布局更改为具有多个列
【发布时间】:2015-05-01 05:19:01
【问题描述】:

我在 MVC5 中使用了脚手架,它为表单创建了标准的默认单列布局。有些表格太长了,不能单列,所以我想把它修改成多列,如图所示。我一直在搞乱,form-group,row,col-md-*,form-inline ...但是格式以一种或另一种方式错位。

解决方案必须非常简单,正确组合 css 标签并与 MVC 框架配合使用。

脚手架标准布局和css

    <div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
        </div>
    </div>

有人可以提供一个示例,说明评论表单的名称、电子邮件和网站,如图所示与视图中的数据绑定一起使用吗?

谢谢

@zgood - 你的回答有一个小问题,不确定你是否知道如何解决它。

  • 我将 div 内的标签与其他文本框一起移动,并删除了提到的水平和内联
  • 当你以某种方式展开它时,带有多个单词的标签会错位。不过只要一个字就好了。有没有快速解决方法?感谢您的回答。

【问题讨论】:

    标签: css asp.net-mvc twitter-bootstrap model-view-controller twitter-bootstrap-3


    【解决方案1】:

    你尝试过这样的事情吗?

        <div class="row">
            <div class="col-sm-4">
                <div class="form-group">
                    @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                    </div>
                </div>
            </div>
    
            <div class="col-sm-4">
                <div class="form-group">
                    @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
                    </div>
                </div>
            </div>
    
            <div class="col-sm-4">
                <div class="form-group">
                    @Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
                    </div>
                </div>
            </div>
        </div>
    

    将您想要的所有字段包装在&lt;div class="row"&gt; 中,然后创建您想要的col-(在本例中为&lt;div class="col-sm-4"&gt;)并将您的form-groups 放在这些列中。另外,我可能会从您的&lt;form&gt; 中删除任何form-horizontalform-inline 引导类

    【讨论】:

    • 针对您的解决方案更新了问题,如果标签超过一个单词,则存在轻微的错位问题...
    • 我想我不需要将标签移动到与我在编辑版本中提到的相同的文本框 div 中。我只是将错误对齐视为必须扩展或调整屏幕大小的固有问题。您的解决方案按原样工作。谢谢。
    • @wirble 当我制作这些表格时,我通常会完全摆脱@Html.LabelFor。我会将placeholder = "Label" 添加到EditorFor 的htmlAttributes 对象中,这比尝试对齐标签和释放屏幕空间更容易。但这通常取决于设计
    • 谢谢,使用您的建议和 html.label 作为标题或字段集效果很好。
    猜你喜欢
    • 2017-04-04
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多