【问题标题】:ASP.NET MVC Bootstrap form controls not aligned properlyASP.NET MVC Bootstrap 表单控件未正确对齐
【发布时间】:2017-01-26 06:50:35
【问题描述】:

在我关注的ASP.NET MVC Core 项目view 中,我想在同一行显示input,其对应的label 和提交按钮Add 控件。但它们显示在三个单独的行中,为什么?。我怎样才能让它们显示在同一行(请注意下拉控件正确在同一行显示其标签和提交按钮Go)。 注意:可能不相关 - 但我们知道标签的内容来自相应模型的数据注释。

查看:

<div class="col-md-9">

    <form asp-controller="myControllerName" asp-action="myTestAction" method="post" class="form-horizontal">
        <hr />
        <div asp-validation-summary="All" class="text-danger"></div>
        <div class="form-group">
            <div class="col-md-9">
                <label asp-for="SelectedYear" class="control-label"></label>
                <select asp-for="SelectedYear" asp-items="Model.lstYears"></select><button type="submit" class="btn btn-info btn-xs">GO</button>
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-9">
                <label asp-for="testDesc" class="control-label"></label>
                <input asp-for="testDesc" class="form-control"/><button type="submit" class="btn btn-info btn-xs">Add</button>
            </div>
        </div>
    </form>
</div>

以上视图的页面显示快照

更新:

根据用户@RatHat 的建议,我已经能够内联显示三个控件。但是,仍然为什么它们不像其他三个下拉控件那样左对齐。新展示:

【问题讨论】:

    标签: asp.net-mvc twitter-bootstrap visual-studio-2015


    【解决方案1】:

    使用.form-horizontal时,需要指定每个元素应覆盖的列数:

    <div class="form-group">
        <!-- add the number of columns that the label should use -->
        <label asp-for="testDesc" class="control-label col-sm-2"></label>
        <!-- add the number of columns that the input should use -->
        <div class="col-sm-8">
            <input asp-for="testDesc" class="form-control">
        </div>
        <!-- add the number of columns that the Add button should use -->
        <div class="col-sm-2">
            <button type="submit" class="btn btn-info btn-xs">Add</button>
        </div>
    </div>
    

    【讨论】:

    • 你的建议是让三个控件在一行中水平显示,但是却把三个控件推到了右边,为什么?请查看我的 UPDATE 部分以及另一张图片。
    • @nam 控件被推到右侧,是由于控件被包裹在 .form-horizo​​ntal 中。由于控件向右对齐是默认行为,因此更容易将列添加到顶部的控件中,以便它们也向右对齐。尝试将所有内容都向左对齐,就像它是正常形式一样,但需要自定义 CSS。
    猜你喜欢
    • 1970-01-01
    • 2015-06-05
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 2015-01-06
    • 2018-07-19
    • 2012-05-22
    • 1970-01-01
    相关资源
    最近更新 更多