Twitter bootstrap MVC引导表单样式设置问题

Twitter bootstrap MVC引导表单样式设置问题,twitter-bootstrap,asp.net-mvc-4,Twitter Bootstrap,Asp.net Mvc 4,我的引导mvc表单有一个小问题。当我添加剃须刀时,表单组的边距似乎不起作用。任何帮助都将不胜感激 这是一个没有正确使用样式的表单 @using (Html.BeginForm("Contact", "Home")) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) <form class="form-horizontal" role="form"> <div class="form-group">

我的引导mvc表单有一个小问题。当我添加剃须刀时,表单组的边距似乎不起作用。任何帮助都将不胜感激

这是一个没有正确使用样式的表单

 @using (Html.BeginForm("Contact", "Home"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<form class="form-horizontal" role="form">
    <div class="form-group">
        <label for="formGroupInputLarge1" class="col-sm-2 control-label">Name</label>
        <div class="col-sm-10">
            @Html.TextBoxFor(m => m.Name, new { @class = "form-control", placeholder = "Fullname", tabindex = 1 })
            @Html.ValidationMessageFor(m => m.Name)
        </div>
    </div>
    <div class="form-group">
        <label for="formGroupInputLarge2" class="col-sm-2 control-label">Email</label>
        <div class="col-sm-10">
            @Html.TextBoxFor(m => m.Email, new { @class = "form-control", placeholder = "Email Address", tabindex = 2 })
            @Html.ValidationMessageFor(m => m.Email)
        </div>
    </div>
    <div class="form-group">
        <label for="formGroupInputLarge3" class="col-sm-2 control-label">Message</label>
        <div class="col-sm-10">
            @Html.TextAreaFor(m => m.Message, new { @class = "form-control", placeholder = "Message", tabindex = 3 })
            @Html.ValidationMessageFor(m => m.Message)
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-primary btn-lg">Send Mail</button>
        </div>
    </div>
</form>
}
@使用(Html.BeginForm(“联系人”、“主页”))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
名称
@TextBoxFor(m=>m.Name,新的{@class=“form control”,placeholder=“Fullname”,tabindex=1})
@Html.ValidationMessageFor(m=>m.Name)
电子邮件
@TextBoxFor(m=>m.Email,新的{@class=“form control”,placeholder=“Email Address”,tabindex=2})
@Html.ValidationMessageFor(m=>m.Email)
消息
@text区域(m=>m.Message,新的{@class=“form control”,placeholder=“Message”,tabindex=3})
@Html.ValidationMessageFor(m=>m.Message)
寄信
}
但一旦我删除了Begin表单,它就会采用正确的样式

<form class="form-horizontal" role="form">
    <div class="form-group">
        <label for="formGroupInputLarge1" class="col-sm-2 control-label">Name</label>
        <div class="col-sm-10">
            @Html.TextBoxFor(m => m.Name, new { @class = "form-control", placeholder = "Fullname", tabindex = 1 })
            @Html.ValidationMessageFor(m => m.Name)
        </div>
    </div>
    <div class="form-group">
        <label for="formGroupInputLarge2" class="col-sm-2 control-label">Email</label>
        <div class="col-sm-10">
            @Html.TextBoxFor(m => m.Email, new { @class = "form-control", placeholder = "Email Address", tabindex = 2 })
            @Html.ValidationMessageFor(m => m.Email)
        </div>
    </div>
    <div class="form-group">
        <label for="formGroupInputLarge3" class="col-sm-2 control-label">Message</label>
        <div class="col-sm-10">
            @Html.TextAreaFor(m => m.Message, new { @class = "form-control", placeholder = "Message", tabindex = 3 })
            @Html.ValidationMessageFor(m => m.Message)
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-primary btn-lg">Send Mail</button>
        </div>
    </div>
</form>

名称
@TextBoxFor(m=>m.Name,新的{@class=“form control”,placeholder=“Fullname”,tabindex=1})
@Html.ValidationMessageFor(m=>m.Name)
电子邮件
@TextBoxFor(m=>m.Email,新的{@class=“form control”,placeholder=“Email Address”,tabindex=2})
@Html.ValidationMessageFor(m=>m.Email)
消息
@text区域(m=>m.Message,新的{@class=“form control”,placeholder=“Message”,tabindex=3})
@Html.ValidationMessageFor(m=>m.Message)
寄信

删除第二个表单并使用:

@using (Html.BeginForm("ActionName", "ControllerName", 
            null, 
            FormMethod.Post, new { @class="form-horizontal" }))
{

您创建的嵌套表单(表单中的表单)无效且不受支持。改变

@using (Html.BeginForm("Contact", "Home"))

并删除第二个表单元素(及其结束标记)


作为旁注,
没有创建正确的
标签
元素,因为
for
属性与表单中的任何元素都没有关系。而是使用
@Html.LabelFor(m=>m.Name,new{@class=“col-sm-2 control label”})
将标签与文本框相关联。


<div class="col-md-8">
        <section id="loginForm">
            @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
            {
                @Html.AntiForgeryToken()
                <h4>Use a local account to log in.</h4>
                <hr />
                @Html.ValidationSummary(true, null, new { @class = "text-danger" })
                <div class="form-group">
                    @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Email, null, new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Password, null, new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <div class="checkbox">
                            @Html.CheckBoxFor(m => m.RememberMe)
                            @Html.LabelFor(m => m.RememberMe)
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Log in" class="btn btn-default" />
                    </div>
                </div>
                <p>
                    @Html.ActionLink("Register as a new user", "Register")
                </p>
                @* Enable this once you have account confirmation enabled for password reset functionality
                    <p>
                        @Html.ActionLink("Forgot your password?", "ForgotPassword")
                    </p>*@
            }
        </section>
    </div>
@使用(Html.BeginForm(“Login”,“Account”,new{ReturnUrl=ViewBag.ReturnUrl},FormMethod.Post,new{@class=“form horizontal”,role=“form”})) { @Html.AntiForgeryToken() 使用本地帐户登录。
@ValidationSummary(true,null,new{@class=“text danger”}) @LabelFor(m=>m.Email,新的{@class=“col-md-2控制标签”}) @TextBoxFor(m=>m.Email,新的{@class=“form control”}) @Html.ValidationMessageFor(m=>m.Email,null,新{@class=“text danger”}) @LabelFor(m=>m.Password,新的{@class=“col-md-2控制标签”}) @Html.PasswordFor(m=>m.Password,新的{@class=“form control”}) @Html.ValidationMessageFor(m=>m.Password,null,新{@class=“text danger”}) @CheckBoxFor(m=>m.RememberMe) @LabelFor(m=>m.RememberMe) @ActionLink(“注册为新用户”、“注册”)

@*为密码重置功能启用帐户确认后启用此选项 @ActionLink(“忘记密码?”,“放弃密码”)

*@ }
您正在打开表单标签两次。检查你的浏览器上的元素,你会看到它。考虑使用。这可能会让你的生活更轻松。请解释一下你做了什么改变以及为什么。也请正确格式化它
<form class="form-horizontal" role="form">
<div class="col-md-8">
        <section id="loginForm">
            @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
            {
                @Html.AntiForgeryToken()
                <h4>Use a local account to log in.</h4>
                <hr />
                @Html.ValidationSummary(true, null, new { @class = "text-danger" })
                <div class="form-group">
                    @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Email, null, new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Password, null, new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <div class="checkbox">
                            @Html.CheckBoxFor(m => m.RememberMe)
                            @Html.LabelFor(m => m.RememberMe)
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Log in" class="btn btn-default" />
                    </div>
                </div>
                <p>
                    @Html.ActionLink("Register as a new user", "Register")
                </p>
                @* Enable this once you have account confirmation enabled for password reset functionality
                    <p>
                        @Html.ActionLink("Forgot your password?", "ForgotPassword")
                    </p>*@
            }
        </section>
    </div>