【问题标题】:.Net MVC to to add html attributes using @HTMLTextAreaFor.Net MVC 使用 @HTMLTextAreaFor 添加 html 属性
【发布时间】: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 =&gt; model.Email, new { htmlAttributes = new { @class = "required-field" } })

工作正常。

任何帮助将不胜感激

【问题讨论】:

  • 只是@Html.TextAreaFor(model =&gt; model.Message, new { @class = "required-field" })
  • 谢谢,效果很好 - 知道为什么 textarea 与编辑器不同吗?
  • EditorFor() 是一种特殊情况(在 MVC-5.1 之前,您甚至无法使用 EditorFor() 添加 html 属性),因为您可以有一个带有多个元素的自定义 EditorTemplate,以便传递值附加到模板中ViewData

标签: c# html .net asp.net-mvc model-view-controller


【解决方案1】:

感谢斯蒂芬·穆克 "它只是 @Html.TextAreaFor(model => model.Message, new { @class= "required-field" })"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-26
    • 2011-12-07
    • 2011-04-21
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多