【问题标题】:MVC5 textarea rows cols work only when specified explicitly and not via htmlAttributesMVC5 textarea rows cols 仅在显式指定且不通过 h​​tmlAttributes 指定时才起作用
【发布时间】:2014-09-04 19:44:15
【问题描述】:

@Html.TextAreaFor(model => model.DESCRIPTION, 20, 50, new { htmlAttributes = new { @class = "form-control"} })

问题:上面的代码正确显示了 textarea 的行和列,但下面的代码忽略了行/列并显示了每个浏览器的默认大小。下面的代码有什么问题?

下面代码中额外的 new 是 VS 在创建 MVC 实体框架控制器时自动创建的(如 http://www.asp.net/mvc/overview/releases/mvc51-release-notes 中所述)

@Html.TextAreaFor(model => model.DESCRIPTION, new { htmlAttributes = new { @class = "form-control", @rows="20", @cols="50"} })

【问题讨论】:

    标签: razor asp.net-mvc-5


    【解决方案1】:

    这适用于 MVC 5.2.2:

    @Html.TextAreaFor(model => model.Caracteristicas, new { @class = "form-control", @rows = "10" })
    

    【讨论】:

      【解决方案2】:

      尝试为 TextAreaFor 使用 mvc5 cols 和 rows 属性,并为文本区域应用 CSS 样式。

              <div class="col-md-10" id="textArea">
                  @Html.TextAreaFor(model => model.DESCRIPTION, 10, 100, new { htmlAttributes = new { @class = "form-control" } })                
              </div>
      

      还有 CSS:

      #textArea textarea {
      max-width: 600px;}
      

      【讨论】:

      • 这对我有用:@Html.TextAreaFor(model => model.WorkDetails, 10, 94, new { @class= "form-control" })
      【解决方案3】:

      在 ASP MVC5 中,您可以将 col-[size]-[width] 代替 @col="50" 用于文本区域或文本框控件。如果您想要比“12”更宽的东西,请使用样式。

      @Html.TextAreaFor(m => m.DESCRIPTION, new { @class = "form-control col-md-12", rows="20" })
      

      【讨论】:

        【解决方案4】:

        您必须使用与@Html.EditorFor 相同的语法才能做到这一点

        @Html.TextAreaFor(model => model.DESCRIPTION, htmlAttributes: new { @class = "form-control", rows=20, cols=50 })
        

        【讨论】:

          【解决方案5】:

          替换这个

          @Html.TextAreaFor(model => model.DESCRIPTION, 20, 50, new { htmlAttributes = new { @class = "form-control"} })
          

          为此

          @Html.TextAreaFor(model => model.DESCRIPTION, 20, 50, htmlAttributes: new { @class = "form-control",@rows="6" })
          

          【讨论】:

          • 请在答案中添加一些解释,这将提高您的回答质量
          • 感谢您的支持
          【解决方案6】:

          我不认为额外的新是你应该能够做到的:

           @Html.TextAreaFor(model => model.DESCRIPTION, new { @class = "form-control", @rows="20", @cols="50" })
          

          【讨论】:

          • 谢谢,我想我想了解 为什么它在 Microsoft 记录和创建时不起作用。
          • 我相信该文档是特定于 EditorFor Html 帮助器的。如果我理解所提供链接中的文档,我不相信各个“控制”助手应该使用该语法。
          猜你喜欢
          • 2011-06-30
          • 2011-01-03
          • 1970-01-01
          • 1970-01-01
          • 2011-09-29
          • 1970-01-01
          • 1970-01-01
          • 2013-05-12
          • 1970-01-01
          相关资源
          最近更新 更多