【问题标题】:I can't adjust the MVC5 Bootstrap TextAreaFor Width我无法调整 MVC5 Bootstrap TextAreaFor 宽度
【发布时间】:2018-02-05 14:55:41
【问题描述】:

我现在已经努力了好几个小时才能在引导选项卡上呈现一个完整的宽度和几行的注释字段。我想要的是这样的:

到目前为止,我所做的是寻找解决方案/帖子,但没有一个对我有用。我的剃须刀代码是:

<div class="tab-pane" id="notes">
<br />
<div class="row">
    <div class="form-group">
        @Html.LabelFor(model => model.Project.sysNotes, new { @class = "col-md-2 control-label" })
        <div class="col-md-8">
            @Html.TextAreaFor(model => model.Project.sysNotes, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Project.sysNotes, "", new { @class = "text-danger" })
        </div>
    </div>
</div>
<div class="row">
    <div class="form-group">
        @Html.LabelFor(model => model.Project.sysUser_Add, new { @class = "col-md-2 control-label" })
        <div class="col-md-4">
            @Html.TextBoxFor(model => model.Project.sysUser_Add, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Project.sysUser_Add, "", new { @class = "text-danger" })
        </div>
        @Html.LabelFor(model => model.Project.sysDD_Add, new { @class = "col-md-2 control-label" })
        <div class="col-md-4">
            @Html.TextBoxFor(model => model.Project.sysDD_Add, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Project.sysDD_Add, "", new { @class = "text-danger" })
        </div>
    </div>
</div>

我将 css 文件(bootstrap、bootstrap-sandstone 和 site)分别调整为:

textarea.form-control {
width: 100%;
height: 200px;}

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

我能做什么???

编辑:另见@T_Roy下方的评论,新的屏幕转储:

也许,错误渲染是否与 _layout.cshtml 有关?

编辑(上次):我接受 T_Roy 的回答,要走的路,但最终解决我当前问题的是 Laiman 的附加组件(hack)。我仍然在我的解决方案中看到奇怪的行为,但它有效并且需要现在继续。但是,我将不得不重构并更多地了解它们是如何协同工作的。

谢谢大家!

【问题讨论】:

  • 尝试将包含的 div 的类设置为col-md-12
  • 更新了解决方案,其中包含覆盖 Bootstrap MVC 表单默认 [div] 类的信息,这些类限制了对象的宽度。

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


【解决方案1】:

您应该实际使用 T_Roy 提到的引导网格、行和列。 See

对于您自己的 css 解决方案,您可以这样做。在TextArea输入中添加类long-width

@Html.TextAreaFor(model => model.Project.sysNotes, new { @class = "form-control long-width" })

然后应用这个 css:

.long-width {
  min-width: 90%;
}

【讨论】:

  • 来曼,我应该把类放在site.css中吗?给定我的 BundleConfig.cs:bundles.Add(new StyleBundle("~/Content/css").Include( "~/Content/bootstrap-sandstone.css", "~/Content/site.css"));
  • 这取决于你,如果你经常需要它,你应该把它放在 site.css 中。如果它是一次性的,您也可以使用内部 css
  • 内部风格起到了作用(或多或少;)。在您看来,我在哪里可以解决这个问题,因为这不是一件容易的事
  • 好的。将它放在 site.css 文件中,并在需要时调用该类。另外,如果这解决了您的问题,那么请将其标记为答案。谢谢。
  • 但是当我把它放在 site.css 中时它不起作用。我真的很困惑。是的,我是 mvc 的新手(还有 razor 和 css 和...)
【解决方案2】:

所以再深入一点,这样你就可以理解它是如何工作的。

首先,row 元素最多可以有 12 列。

Documentation

例如,使用您提供的内容:

<div class="row">
    <div class="form-group">
        @Html.LabelFor(model => model.Project.sysUser_Add, new { @class = "col-md-2 control-label" })
        <div class="col-md-4">
            @Html.TextBoxFor(model => model.Project.sysUser_Add, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Project.sysUser_Add, "", new { @class = "text-danger" })
        </div>
        @Html.LabelFor(model => model.Project.sysDD_Add, new { @class = "col-md-2 control-label" })
        <div class="col-md-4">
            @Html.TextBoxFor(model => model.Project.sysDD_Add, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Project.sysDD_Add, "", new { @class = "text-danger" })
        </div>
    </div>
</div>

您的 labels 每个都有一个名为 col-md-2 的类,这意味着它们在一行中占据 2 列。所以既然你有 2 个标签(model.Project.sysUser_Addmodel.Project.sysDD_Add),那么你真的占用了 4 列。因此,在该行被填满之前,您还有 8 列。因此,您的 TextBoxFor 占这 8 列,因为您的 TextBoxFor 每个都在 div 内,其中有名为 col-md-4 的类。 4 + 4 = 8。

现在采用该逻辑并将其应用于您的TextAreaFor

您有一个标签,其类为col-md-2,因此在该行被填满之前您还有 10 列。截至目前,您的TextAreaFor 位于div 中,即col-md-88 + 2 = 10。在填充该行之前,您仍有 2 列可供使用。因此,将您的TextAreaFor 所在的div 更改为col-md-1010 + 2 = 12。这应该调整您的 TextAreaFor 以使用全角来匹配您的 TextBoxFors。

这是一个有效的demo

如果这有帮助,请告诉我!

【讨论】:

  • 嗨@T_Roy,玩了一段时间后,我删除了这个例子:&lt;div class="container"&gt; &lt;div class="row"&gt; &lt;div class="form-group"&gt; @Html.LabelFor(model =&gt; model.Project.sysNotes, new { @class = "col-md-2 control-label" }) @Html.TextAreaFor(model =&gt; model.Project.sysNotes, new { @class = "form-control col-md-10" }) &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; ,即使这样也不起作用。为问题添加屏幕截图
  • 您将col-md-10 放在错误的位置。 @Html.TextAreaFor 不需要任何处理col 的类。在您的原始示例中..您在TextAreaFor 上方有一个div,即col-md-8.. 将其更改为col-md-10
  • 当然希望demo 链接仍然有效。 (好吧,我希望它对我有用。也许是我的工作箱无法使用它。)
【解决方案3】:

col-md-8 更改为col-md-10 将变为width: 100%

【讨论】:

  • 如果 OP 将列设置为col-md-12,那么它们的标签将不再位于文本区域的一侧。它会在它上面,因为 textarea 现在占据了row的整个空间
  • 抱歉,应该是 10
  • 你有托管过吗?还是住?
【解决方案4】:

根据文档 (Link),行类应该在容器类中。

【讨论】:

  • 此答案无助于 OP 找到解决问题的方法。
【解决方案5】:

VS 2017 解决方案

如果您只想定位一个对象,可以直接将其添加到新的 {} 属性中。textarea 宽度由 cols 属性控制,因此原始 HTML 会将基于百分比的项目平衡到 100%,因此 40-40 -40% 相当于它们所在对象的 33-33-33% 或 ~100%。

@Html.TextAreaFor(model => model.Project.sysNotes
    , new { @class = "form-control", @rows="3",  @cols = "40%" })

引导

Bootstrap 完全是另一种动物。我花了几天时间才弄清楚是什么限制了生成表单中的对象的宽度。

3 个相关项目。可以引用与生成的 [div] 相关的:.form-control、.form-group & .form-row。 *** 我将所有表单组对象切换为表单行对象,因为它们在行之间留下的空间比表单组对象少,因此滚动更少。

将此添加到您的样式表中以允许最大宽度。然后您可以覆盖要缩小的 [div] 的宽度。

<style>
    .form-control {
        width: 100%;
        max-width: 100%;
    }

    .form-group {
        width: 100%;
        max-width: 100%;
    }

    .form-row {
        width: 100%;
        max-width: 100%;
    }

</style>

然后覆盖您希望缩小的 [div] 中的代码。

<div class="form-row" style="width: 320px">

【讨论】:

    猜你喜欢
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    • 1970-01-01
    • 2013-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多