如何使用form-inline bootstrap 3?

时间:2015-08-25 14:21:55

标签: html css forms twitter-bootstrap razor

我在HTML和Razor中有一个视图,我正在尝试构建一个如下所示的表单: mockup image

然而,我能达到的最好成绩如下:

result image

如您所见,标签未内联。 为了达到这个目的,我已经阅读了Bootstrap 3(http://getbootstrap.com/components/#navbar-forms)中的form-inline类,但无论我如何使用form-inline,我都无法将标签放到正确的位置而不会使整个布局变得混乱。

这是我当前的代码(没有form-inline):

 <fieldset class="group-border">
                    <legend class="group-border">Info</legend>

                    <div class="row">
                        <div class="col-lg-4">
                            <div class="form-group">
                                @Html.LabelFor(doc => doc.NumDoc, new { @class = "control-label" })
                                @Html.TextBoxFor(doc => doc.NumDoc, new { @class = "form-control input-sm", placeholder = "Nº Documento", disabled = "disabled" })
                            </div>
                        </div>
                        <div class="col-lg-4">
                            <div class="form-group">
                                @Html.LabelFor(doc => doc.ProcessNum, new { @class = "control-label" })
                                @Html.TextBoxFor(doc => doc.ProcessNum, new { @class = "form-control input-sm", placeholder = "Nº Processo", disabled = "disabled" })
                            </div>
                        </div>
                        <div class="col-lg-4">
                            <div class="form-group">
                                @Html.LabelFor(doc => doc.State, new { @class = "control-label" })
                                @Html.TextBoxFor(doc => doc.State, new { @class = "form-control input-sm", placeholder = "Estado", disabled = "disabled" })
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-lg-6">
                            <div class="form-group">
                                @Html.LabelFor(doc => doc.Name, new { @class = "control-label" })
                                @Html.TextBoxFor(doc => doc.Name, new { @class = "form-control input-sm", placeholder = "Nome Documento", disabled = "disabled" })
                            </div>
                        </div>
                        <div class="col-lg-6">
                            @Html.LabelFor(doc => doc.IsArchived, new { @class = "control-label" })
                            @Html.CheckBoxFor(doc => doc.IsArchived, new { @class = "input-sm", disabled = "disabled" })
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-lg-6">
                            <div class="form-group">
                                @Html.LabelFor(doc => doc.Description, new { @class = "control-label" })
                                @Html.TextBoxFor(doc => doc.Description, new { @class = "form-control input-sm", placeholder = "Descrição", disabled = "disabled" })
                            </div>
                        </div>
                        <div class="col-lg-6">
                            <div class="form-group">
                                @Html.LabelFor(doc => doc.Date, new { @class = "control-label" })
                                @Html.TextBoxFor(doc => doc.Date, new { @class = "form-control input-sm", placeholder = "dd/MM/yyyy", disabled = "disabled" })
                            </div>
                        </div>
                    </div>

                </fieldset>

Bellow是HTML CSS和Javascript中的代码片段,试图复制我所拥有的内容。

<style> legend.group-border {
  width: inherit;
  /* Or auto */
  padding: 0 10px;
  /* To give a bit of padding on the left and right */
  border-bottom: none;
}
fieldset.group-border {
  border: 1px groove #ddd !important;
  padding: 0 1.4em 1.4em 1.4em !important;
  margin: 0 0 1.5em 0 !important;
  -webkit-box-shadow: 0px 0px 0px 0px #000;
  box-shadow: 0px 0px 0px 0px #000;
}
</style>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<fieldset class="group-border">
  <legend class="group-border">Info</legend>

  <div class="row">
    <div class="col-lg-4">
      <div class="form-group">
        <label class="control-label" for="NumDoc">Nº Documento</label>
        <input class="form-control input-sm" data-val="true" data-val-number="The field Nº Documento must be a number." data-val-required="O campo Nº Documento é necessário." disabled="disabled" id="NumDoc" name="NumDoc" placeholder="Nº Documento" type="text"
        value="21354">
      </div>
    </div>
    <div class="col-lg-4">
      <div class="form-group">
        <label class="control-label" for="ProcessNum">Nº Processo</label>
        <input class="form-control input-sm" data-val="true" data-val-number="The field Nº Processo  must be a number." data-val-required="O campo Nº Processo  é necessário." disabled="disabled" id="ProcessNum" name="ProcessNum" placeholder="Nº Processo" type="text"
        value="54463">
      </div>
    </div>
    <div class="col-lg-4">
      <div class="form-group">
        <label class="control-label" for="State">Estado</label>
        <input class="form-control input-sm" disabled="disabled" id="State" name="State" placeholder="Estado" type="text" value="Em Inbox">
      </div>
    </div>
  </div>

  <div class="row">
    <div class="col-lg-6">
      <div class="form-group">
        <label class="control-label" for="Name">Nome</label>
        <input class="form-control input-sm" disabled="disabled" id="Name" name="Name" placeholder="Nome Documento" type="text" value="A134-MULTAS">
      </div>
    </div>
    <div class="col-lg-6">
      <label class="control-label" for="IsArchived">Arquivado?</label>
      <input checked="checked" class="input-sm" data-val="true" data-val-required="O campo Arquivado? é necessário." disabled="disabled" id="IsArchived" name="IsArchived" type="checkbox" value="true">
      <input name="IsArchived" type="hidden" value="false">
    </div>
  </div>

  <div class="row">
    <div class="col-lg-6">
      <div class="form-group">
        <label class="control-label" for="Description">Descrição</label>
        <input class="form-control input-sm" disabled="disabled" id="Description" name="Description" placeholder="Descrição" type="text" value="Fast and Furious">
      </div>
    </div>
    <div class="col-lg-6">
      <div class="form-group">
        <label class="control-label" for="Date">Data Documento</label>
        <input class="form-control input-sm" data-val="true" data-val-date="The field Data Documento must be a date." data-val-required="O campo Data Documento é necessário." disabled="disabled" id="Date" name="Date" placeholder="dd/MM/yyyy" type="text" value="25/08/2015 15:03:26">
      </div>
    </div>
  </div>

</fieldset>

正如你所看到的,我有一排,分为3列(每一列占据4个单位的宽度),里面我使用Razor制作表格。

如何修改我当前的HTML代码以在模型中实现结果?

1 个答案:

答案 0 :(得分:4)

以下是代码段,我刚刚将form-group类更改为form-inline,更改了列数及其宽度,只是为了使其工作添加containerrowcol-md-12 div,我也尝试垂直对齐checkbox,看起来更像是您的第一个模型图像

<style> legend.group-border {
  width: inherit;
  /* Or auto */
  padding: 0 10px;
  /* To give a bit of padding on the left and right */
  border-bottom: none;
}
fieldset.group-border {
  border: 1px groove #ddd !important;
  padding: 0 1.4em 1.4em 1.4em !important;
  margin: 0 0 1.5em 0 !important;
  -webkit-box-shadow: 0px 0px 0px 0px #000;
  box-shadow: 0px 0px 0px 0px #000;
}
</style>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
            <div class="row">
                <div class="col-md-12">
                    <fieldset class="group-border">
                        <legend class="group-border">Info</legend>

                        <div class="row">
                            <div class="col-lg-4">
                                <div class="form-inline">
                                    <label class="control-label" for="NumDoc">Nº Documento</label>
                                    <input class="form-control input-sm" data-val="true" data-val-number="The field Nº Documento must be a number." data-val-required="O campo Nº Documento é necessário." disabled="disabled" id="NumDoc" name="NumDoc" placeholder="Nº Documento" value="21354" type="text">
                                </div>
                            </div>
                            <div class="col-lg-4">
                                <div class="form-inline">
                                    <label class="control-label" for="ProcessNum">Nº Processo</label>
                                    <input class="form-control input-sm" data-val="true" data-val-number="The field Nº Processo  must be a number." data-val-required="O campo Nº Processo  é necessário." disabled="disabled" id="ProcessNum" name="ProcessNum" placeholder="Nº Processo" value="54463" type="text">
                                </div>
                            </div>
                            <div class="col-lg-4">
                                <div class="form-inline">
                                    <label class="control-label" for="State">Estado</label>
                                    <input class="form-control input-sm" disabled="disabled" id="State" name="State" placeholder="Estado" value="Em Inbox" type="text">
                                </div>
                            </div>
                        </div>
                        <br>
                        <div class="row">
                            <div class="col-lg-1">
                                <label class="control-label" for="Name">Nome</label>
                            </div>
                            <div class="col-lg-7">
                                <input class="form-control input-sm" disabled="disabled" id="Name" name="Name" placeholder="Nome Documento" value="A134-MULTAS" type="text">
                            </div>
                            <div style="float: left;" class="col-lg-4 text-left">
                                <input style="float: left;" checked="checked" class="input-sm" data-val="true" data-val-required="O campo Arquivado? é necessário." disabled="disabled" id="IsArchived" name="IsArchived" value="true" type="checkbox">
                                <label style="margin-top: 10px; margin-left: 9px;" class="control-label" for="IsArchived">Arquivado?</label>
                                <input name="IsArchived" value="false" type="hidden">
                            </div>
                        </div>
                        <br>
                        <div class="row">
                            <div class="col-lg-1">
                                <label class="control-label" for="Description">Descrição</label>
                            </div>
                            <div class="col-lg-7">
                                <input class="form-control input-sm" disabled="disabled" id="Description" name="Description" placeholder="Descrição" value="Fast and Furious" type="text">
                            </div>
                            <div class="col-lg-4">
                                <div class="form-inline">
                                    <label class="control-label" for="Date">Data Documento</label>
                                    <input class="form-control input-sm" data-val="true" data-val-date="The field Data Documento must be a date." data-val-required="O campo Data Documento é necessário." disabled="disabled" id="Date" name="Date" placeholder="dd/MM/yyyy" value="25/08/2015 15:03:26" type="text">
                                </div>
                            </div>
                        </div>
                    </fieldset>
                </div>
            </div>
        </div>