【问题标题】:Control alignment in Bootstrap 4Bootstrap 4 中的控制对齐
【发布时间】:2020-06-03 02:18:46
【问题描述】:

我正在尝试学习 MVC。脱离 Visual Studio 所做的脚手架,一切都运行良好。但是当更新到 Bootstrap 4 时,我的所有控件都在视图中打乱了。我设法按照 getbootstrap.com 迁移步骤解决了其中大部分问题,但仍有一些问题

可见,Country DropDown 和 DatePicker 没有到位。这是我的视图代码:

  <div class="input-group" id="Country">
     @Html.LabelFor(model => model.IDCountry, htmlAttributes: new { @class = "col-md-4 col-lg-4 text-right" })
     <div class="col-md-6 col-lg-6">
        @Html.DropDownListFor(model => model.IDCountry, new SelectList(Country.List(), "Key", "Value"), new { style = "height: 30px" })
        @Html.ValidationMessageFor(model => model.IDCountry, "", new { @class = "text-danger" })
     </div>
  </div>
  <br />


  <div class="form-group">
     @Html.LabelFor(model => model.HolidayDate, htmlAttributes: new { @class = "col-md-4 col-lg-4" })
     <div class="col-md-8 col-lg-8">
        @Html.EditorFor(model => model.HolidayDate, new { autocomplete = "off" })
        @Html.ValidationMessageFor(model => model.HolidayDate, "", new { @class = "text-danger" })
     </div>
  </div>

所有内容都包含在 form class="form-inline"

一切都在列中,以 Xl 大小运行,所以,我不知道 在此先感谢您的帮助

EDIT:视图的完整代码

 @model Trevo.Models.Holidays

@{
   ViewBag.Title = "Edit";
}

@*<link rel="stylesheet" href="~/Content/themes/base/jquery-ui.css" />*@

<h2>Edit</h2>

@using (Html.BeginForm())
{
   @Html.AntiForgeryToken()

   <form class="form-inline">
      <h4>Holidays</h4>
      <hr />
      @Html.ValidationSummary(true, "", new { @class = "text-danger" })

      @* To enable or disable the country list *@
      <script type="text/javascript">
         function ShowHideCountry(SelectGlobal) {
            var dvCountry = document.getElementById("Country");
            dvCountry.style.display = SelectGlobal.checked ? "none" : "block";
         }
      </script>

      <div class="input-group" id="IsGlobal">
         @Html.LabelFor(model => model.IsGlobal, htmlAttributes: new { @class = "col-md-4 col-lg-4 text-right", style = "display: inline-block" })
         <div class="col-md-6 col-lg-6">
            @Html.CheckBoxFor(model => model.IsGlobal, new { id = "SelectGlobal", onclick = "ShowHideCountry(this)", style = "width:18px;height:18px;display: inline-block" })
            @Html.ValidationMessageFor(model => model.IsGlobal, "", new { @class = "text-danger" })
         </div>
      </div>
      <br />

      <div class="input-group col-md-3 col-lg-3" id="Country">
         @Html.LabelFor(model => model.IDCountry, htmlAttributes: new { @class = "col-md-4 col-lg-4 text-right" })
         <div class="col-md-6 col-lg-6">
            @Html.DropDownListFor(model => model.IDCountry, new SelectList(Country.List(), "Key", "Value"), new { style = "height: 30px" })
            @Html.ValidationMessageFor(model => model.IDCountry, "", new { @class = "text-danger" })
         </div>
      </div>
      <br />

      @Html.HiddenFor(model => model.IDHoliday)

      @*<div class="input-group" id="HolidayDate">
            @Html.LabelFor(model => model.HolidayDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-6 col-lg-6">
               @Html.EditorFor(model => model.HolidayDate, new { autocomplete = "off" })
               @Html.ValidationMessageFor(model => model.HolidayDate, "", new { @class = "text-danger" })
            </div>
         </div>
         <br />*@

      <div class="form-group">
         @Html.LabelFor(model => model.HolidayDate, htmlAttributes: new { @class = "col-md-4 col-lg-4 text-right" })
         <div class="col-md-8 col-lg-8">
            @Html.EditorFor(model => model.HolidayDate, new { autocomplete = "off" })
            @Html.ValidationMessageFor(model => model.HolidayDate, "", new { @class = "text-danger" })
         </div>
      </div>

      <div class="input-group" id="Annual">
         @Html.LabelFor(model => model.Annual, htmlAttributes: new { @class = "col-md-4 col-lg-4 text-right" })
         <div class="col-md-6 col-lg-6">
            @Html.DropDownListFor(model => model.Annual, new[] { new SelectListItem { Text = "No", Value = "0" }, new SelectListItem { Text = "Yes", Value = "1" } }, new { style = "height: 30px" })
            @Html.ValidationMessageFor(model => model.Annual, "", new { @class = "text-danger" })
         </div>
      </div>
      <br />

      <div class="input-group" id="Name">
         @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "col-md-4 col-lg-4 text-right" })
         <div class="col-md-6 col-lg-6">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
         </div>
      </div>
      <br />

      <div class="input-group" id="Workable">
         @Html.LabelFor(model => model.Workable, htmlAttributes: new { @class = "col-md-4 col-lg-4 text-right" })
         <div class="col-md-6 col-lg-6">
            @Html.DropDownListFor(model => model.Workable, new[] { new SelectListItem { Text = "No", Value = "0" }, new SelectListItem { Text = "Yes", Value = "1" } }, new { style = "height: 30px" })
            @Html.ValidationMessageFor(model => model.Workable, "", new { @class = "text-danger" })
         </div>
      </div>

      <div class="col text-center">
         <input type="submit" value="Save" class="btn btn-primary" />
      </div>
      <hr />

   </form>
}

<div>
   <div class="row">
      <div class="col text-right">
         @Html.ActionLink("Back to List", "Index", null, new { @class = "btn btn-success" })
      </div>
   </div>
</div>

@section Scripts {
   @Scripts.Render("~/bundles/jqueryval")

   @* To enable or disable the country list on Page load *@
   <script type="text/javascript">
      $(document).ready(function () {
         var dvCountry = document.getElementById("Country");
         dvCountry.style.display = SelectGlobal.checked ? "none" : "block";
      });
   </script>

   @* DateTime picker *@
   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
   <script src="~/Scripts/jquery.maskedinput.min.js"></script>
   <script>
      $("#HolidayDate").datepicker({ dateFormat: "dd/mm/yy" }).mask("99/99/9999");
      $.validator.addMethod('date',
         function (value, element, params) {
            if (this.optional(element)) {
               return true;
            }
            var ok = true;
            try {
               $.datepicker.parseDate('dd/mm/yy', value);
            }
            catch (err) {
               ok = false;
            }
            return ok;
         });
   </script>
}

【问题讨论】:

    标签: asp.net-mvc bootstrap-4


    【解决方案1】:

    您需要进行以下 2 项更改才能使其正常工作:

    从 IDCountry 组中删除 col-md-3 col-lg-3

    <div class="input-group col-md-3 col-lg-3" id="Country">
    

    应该是:

    <div class="input-group" id="Country">
    

    将 HolidayDate 组从 form-group 更改为 input-group

    <div class="form-group">
    @Html.LabelFor(model => model.HolidayDate, htm...
    

    应该是:

    <div class="input-group">
    @Html.LabelFor(model => model.HolidayDate, htm...
    

    【讨论】:

    • 同样的结果。不管怎么说,还是要谢谢你。其他事情正在发生。我试图将它们包含在 ROW 类中,也没有工作。
    • 确实很奇怪。我使用上述更改运行了您的代码,并且所有这些都完美对齐。
    • 再次感谢 Lasse。我现在的解决方案是回到 Bootstrap 3
    猜你喜欢
    • 1970-01-01
    • 2017-09-04
    • 2019-04-12
    • 2017-05-07
    • 2018-10-02
    • 2019-09-23
    • 2020-03-11
    • 1970-01-01
    • 2017-05-11
    相关资源
    最近更新 更多