简体   繁体   English

如何在MVC视图中使用Bootstrap按钮插件

[英]How can I use Bootstrap button addons in my MVC view

I'm having difficulty getting Bootstrap's button addons to work in my MVC view. 我很难让Bootstrap的按钮插件在我的MVC视图中工作。 I'm using the latest NuGet version of ASP.NET MVC (5.1 rc1) and Bootstrap (3.03). 我正在使用最新的NuGet版本的ASP.NET MVC(5.1 rc1)和Bootstrap(3.03)。

I have the following in my view (now that I've pared it back to just hand-coded HTML rather than using Html.EditorFor() , in an attempt to getting it to work): 我在视图中有以下内容(现在我已经将其Html.EditorFor()为手工编码的HTML而不是使用Html.EditorFor() ,以试图让它工作):

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

    <div class="form-horizontal">
        @Html.ValidationSummary(true)

        <div class="row">
            <div class="col-lg-6">
                <div class="input-group">
                    <input type="text" class="form-control" />
                    <span class="input-group-btn">
                        <button class="btn btn-default" type="button">Go!</button>
                    </span>
                </div>
            </div>
            <div class="col-lg-6">
                &nbsp;
            </div>
        </div>

This generates the following HTML: 这会生成以下HTML:

<form action="xxx" method="post">
<input name="__RequestVerificationToken" type="hidden" value="T3k..." />
<div class="form-horizontal">
    <div class="row">
        <div class="col-lg-6">
            <div class="input-group">
                <input type="text" class="form-control" />
                <span class="input-group-btn">
                    <button class="btn btn-default" type="button">Go!</button>
                </span>
            </div>
        </div>
        <div class="col-lg-6">
            &nbsp;
        </div>
    </div>
</div>
</form>

The problem is that, when this is displayed in the browser (Chrome 32 / IE 11), there's a big gap between the input box and the button. 问题是,当它在浏览器(Chrome 32 / IE 11)中显示时,输入框和按钮之间存在很大差距。 Like this: 像这样: 在此输入图像描述

If I reduce the size of the div surrounding the input-group div to col-lg-3 or smaller, it's fine. 如果我减少大小div围绕input-group divcol-lg-3或更小,它的罚款。 But anything larger than that leaves a gap. 但任何大于此的东西都会留下空隙。

It's as though there's a maximum size on the input - and indeed, all my inputs do seem to be smaller their container div ... 就好像有一个在最大尺寸input -事实上,我所有的inputs都似乎是他们的小容器div ...

What could be causing this? 可能是什么导致了这个?

The default Site.css stylesheet that comes with a new MVC 5 project has a rule that limits the max-width of inputs. 新MVC 5项目附带的默认Site.css样式表具有限制输入最大宽度的规则。 What you're getting is the result of the control spanning the full available width like it's supposed to, but then the input, itself, is being constrained to a defined width. 你得到的是控制的结果,它跨越了它应该的全部可用宽度,但是输入本身被约束到一个定义的宽度。 Just comment out that piece of the stylesheet and everything will work as it should. 只需注释掉那段样式表,一切都会按原样运作。

/* Set width on the form input elements since they're 100% wide by default */
input,
select,
textarea {
    max-width: 280px;
}

It's a pretty egregious shortcut the team seems to have taken in order to quickly build the sample site. 这是团队为了快速构建示例站点而采取的相当令人震惊的捷径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM