MVC5表单与Bootstrap

时间:2016-02-10 13:51:16

标签: twitter-bootstrap-3 asp.net-mvc-5

当我恢复窗口时,我的表单看起来很好: enter image description here

但是,如果我跨越窗户,它看起来不太好: enter image description here

我的观看代码是:

@using System.Collections
@using System.Collections.Concurrent
@using System.Data.SqlTypes
@using DataAccess
@using Recaptcha.Web.Mvc
@using Recaptcha.Web;
@using Recaptcha.Web.Mvc;

@model DataAccess.Employee

@{
ViewBag.Title = "Create";
}
     @{ Layout = "/views/shared/_EmployeeLayout.cshtml";
}


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

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

<div class="container">

    <h2>Please, provide some information.</h2>

    <div class="row">

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

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


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

                @Html.LabelFor(model => model.Location, "Location", htmlAttributes: new { @class = "control-label col-md-2" })
                @Html.DropDownList("LocationID", null, "--Please pick one--", new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Location, "", new { @class = "text-danger" })


                @Html.LabelFor(model => model.Mail, "E-mail", htmlAttributes: new { @class = "control-label col-md-2" })
                @Html.EditorFor(model => model.Mail, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Mail, "", new { @class = "text-danger" })


                @Html.LabelFor(model => model.Telephone, "Telephone number", htmlAttributes: new { @class = "control-label col-md-2" })
                @Html.EditorFor(model => model.Telephone, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Telephone, "", new { @class = "text-danger" })

                @Html.LabelFor(model => model.StartEduYear, "Entrance study year", htmlAttributes: new { @class = "control-label col-md-2" })
                @Html.DropDownListFor(model => model.StartEduYear, ViewBag.StartYearFaculty as IEnumerable<SelectListItem>, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.StartEduYear, "", new { @class = "text-danger" })

                @Html.LabelFor(model => model.EnglishLang.NameLang, "English level", htmlAttributes: new { @class = "control-label col-md-2" })
                @Html.DropDownList("EngID", null, "--Choose level--", new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.EngID, "", new { @class = "text-danger" })

                @Html.LabelFor(model => model.Faculty.FacultyName, "Faculty", htmlAttributes: new { @class = "control-label col-md-2" })
                @Html.DropDownListFor(model => model.FacultyID, ViewBag.FacultyID as IEnumerable<SelectListItem>, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.FacultyID, "", new { @class = "text-danger" })





                @Html.LabelFor(model => model.AvgScore, "Average score", htmlAttributes: new { @class = "control-label col-md-2" })
                @Html.DropDownListFor(model => model.AvgScore, ViewBag.AverScore as IEnumerable<SelectListItem>, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.AvgScore, "", new { @class = "text-danger" })


                @Html.LabelFor(model => model.GradYear, "Graduation year", htmlAttributes: new { @class = "control-label col-md-2" })
                @Html.DropDownListFor(model => model.GradYear, ViewBag.GradYear as IEnumerable<SelectListItem>, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.GradYear, "", new { @class = "text-danger" })



                @Html.LabelFor(model => model.WExp, "Work Experience", htmlAttributes: new { @class = "control-label col-md-2" })
                @Html.TextAreaFor(model => model.WExp, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.WExp, "", new { @class = "text-danger" })


                @Html.LabelFor(model => model.Interests, "Interests", htmlAttributes: new { @class = "control-label col-md-2" })
                @Html.TextAreaFor(model => model.Interests, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Interests, "", new { @class = "text-danger" })






                <p></p>

                <span>Did you have any additional trainings?</span>
                <label for="chkYes">
                    <input type="radio" id="chkYes" name="chkPassPort" onclick=" ShowHideDiv() " />Yes
                </label>
                <label for="chkNo">
                    <input type="radio" id="chkNo" name="chkPassPort" onclick=" ShowHideDiv() " />No
                </label>

                <div class="form-group" id="dvPassport" style="display: none">
                    @Html.LabelFor(model => model.Trainings, "Trainings", htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.TextAreaFor(model => model.Trainings, new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => model.Trainings, "", new { @class = "text-danger" })
                    </div>
                </div>
                <p></p>
                <span>Did you have any projects recently?</span>
                <label for="chkYes">
                    <input type="radio" id="chkYes1" name="chkPassPort1" onclick=" ShowHideDiv() " />Yes
                </label>
                <label for="chkNo">
                    <input type="radio" id="chkNo1" name="chkPassPort1" onclick=" ShowHideDiv() " />No
                </label>

                <div class="form-group" id="dvExample" style="display: none">
                    @Html.LabelFor(model => model.Projects, "Projects", htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.TextAreaFor(model => model.Projects, new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => model.Projects, "", new { @class = "text-danger" })
                    </div>
                </div>
                <p></p>
                <span>Have you been in some students organization?</span>
                <label for="chkYes">
                    <input type="radio" id="chkYes2" name="chkPassPort2" onclick=" ShowHideDiv() " />Yes
                </label>
                <label for="chkNo">
                    <input type="radio" id="chkNo2" name="chkPassPort2" onclick=" ShowHideDiv() " />No
                </label>

                <div class="form-group" id="dvExample1" style="display: none">
                    @Html.LabelFor(model => model.StudOrg, "Organizations", htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.TextAreaFor(model => model.StudOrg, new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => model.StudOrg, "", new { @class = "text-danger" })
                    </div>
                </div>
            </div>

            <div class="form-group">
                <div class="g-recaptcha" data-sitekey="6LccVRcTAAAAADLo_LxSj4mQzt9jLrgbo5L9wZ-J"></div>
                <div>
                    @if (TempData["recaptcha"] != null)
                    {
                        <p>@TempData["recaptcha"]</p>
                    }
                </div>
            </div>
            <br />
            <br />
            <div class="col-md-offset-2 col-md-10">

                <b></b>
                <div class="form-group">
                    @Html.Recaptcha()
                </div>
                <br />
                <br />
                <br />
                <br />
                <input type="submit" value="Apply" class="btn btn-default" style="text-align: center" />
            </div>

        </div>
        <p></p>

    </div>
</div>
}

<script type="text/javascript">
function ShowHideDiv() {
    var chkYes = document.getElementById("chkYes");
    var chkYes1 = document.getElementById("chkYes1");
    var chkYes2 = document.getElementById("chkYes2");

    var dvPassport = document.getElementById("dvPassport");
    var dvExample = document.getElementById("dvExample");
    var dvExample1 = document.getElementById("dvExample1");

    dvPassport.style.display = chkYes.checked ? "block" : "none";
    dvExample.style.display = chkYes1.checked ? "block" : "none";
    dvExample1.style.display = chkYes2.checked ? "block" : "none";

 }
</script>

我也将每个字段分开,主要字段位于第10列,但即便如此,我的字段也相互折叠..

1 个答案:

答案 0 :(得分:0)

将这一行分开放置

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