MVC5 textarea的行数和列数只有在显式指定而不是通过htmlAttributes指定时才起作用。

4

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

问题:上述代码可以正确地显示文本区域的行数和列数,但下面的代码却忽略了行/列并根据浏览器显示默认大小。下面的代码哪里出错了?

下面代码中的额外的new是由VS在创建MVC Entity Framework控制器时自动创建的(如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"} })

6个回答

6

这适用于MVC 5.2.2:

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

3
尝试使用mvc5的cols和rows属性来为TextAreaFor定义行和列,同时为文本区应用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" }) - NMeneses

1
在ASP MVC5中,您可以使用col-[size]-[width]代替@col="50"来控制文本框或文本域的宽度。如果您需要比“12”更宽的元素,请使用样式。
@Html.TextAreaFor(m => m.DESCRIPTION, new { @class = "form-control col-md-12", rows="20" })

0

这是可以做到的,您需要使用与 @Html.EditorFor 相同的语法。

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

0

替换这个

@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" })

1
请在答案中添加一些解释,这将提高您的答案质量。 - DaFois
感谢您的支持。 - Alex Sanchez

-1

我不认为额外的new是必要的,你应该只需要这样做:

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

谢谢,我想我正在尝试理解为什么它既有文档支持又是由微软创建的,但却无法正常工作。 - joym8
1
我认为该文档具体适用于EditorFor Html助手。如果我正确理解所提供链接中的文档,我不认为各个“Control”助手旨在使用该语法。 - mreyeros

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接