【发布时间】:2023-04-01 14:53:01
【问题描述】:
在使用@HTML 命令时,我无法将一些 html 属性添加到文本区域。我只是想添加一个类名,但它不起作用。
这是我的代码:
@Html.TextAreaFor(model => model.Message, new { htmlAttributes = new { @class = "required-field" } })
它正在生成:
<textarea id="Message" rows="2" name="Message" htmlattributes="{ class = required-field }" data-val-required="The Message field is required." data-val="true" cols="20"></textarea>
我不确定为什么它显示 htmlattributes="{ class= required-field }" 而不是 class="required-field" 因为相同的代码适用于普通文本输入 - 即
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "required-field" } })
工作正常。
任何帮助将不胜感激
【问题讨论】:
-
只是
@Html.TextAreaFor(model => model.Message, new { @class = "required-field" }) -
谢谢,效果很好 - 知道为什么 textarea 与编辑器不同吗?
-
EditorFor()是一种特殊情况(在 MVC-5.1 之前,您甚至无法使用EditorFor()添加 html 属性),因为您可以有一个带有多个元素的自定义EditorTemplate,以便传递值附加到模板中ViewData
标签: c# html .net asp.net-mvc model-view-controller