TextAreaFor Not Working When Using HtmlAttributes Overload

◇◆丶佛笑我妖孽 提交于 2020-02-06 16:54:46

问题


For some reasons, when I set my TextAreaFor style properties specifically the width property, it is not working, what I mean is the width is not changing, and the Id is not changing as well:

 @Html.TextAreaFor(model => model.AdminSetting.Style, new { htmlAttributes = new 
 { @class = "form-control", @style = "width: 100%;", @id = "HelloWorld" } })

But if I remove the word htmlAttributes and change it to this then it working fine. I really wonder why:

@Html.TextAreaFor(model => model.AdminSetting.Style, 
new { @class = "form-control", @style = "width: 100%;", @id = "HelloWorld" })

Is there reason why my TextAreaFor htmlAttributes not working unless I remove my declaration of htmlAttributes and declare it like this?

 new { @class = "form-control", @style = "width: 100%;", @id = "HelloWorld" })

Instead of this?

new { htmlAttributes = new 
 { @class = "form-control", @style = "width: 100%;", @id = "HelloWorld" } })

I've check the documentation, and I am sure that I am using the correct overload.


回答1:


Try as shown below:

@Html.TextAreaFor(m => m.AdminSetting.Style, 
    new { maxlength = 1000, @class = "form-control", @id = "HelloWorld" })

The helpers, Html.TextAreaFor, Html.EditorFor, Html.DisplayFor are unique in that they're what's referred to as "templated helpers". Instead of just spitting out some defined bit of HTML, they're capable of actually using views to determine what their output will be. These views are referred to as "editor templates" or "display templates", respectively. For more information please have a look at Html.EditorFor and htmlAttributes.

Hope this helps...




回答2:


Syntax:

new { htmlAttributes = new { @class = "form-control", @style = "width: 100%;", @id = "HelloWorld" } })

represents additional data for ViewData and can be used with EditorFor.

See: MVC View htmlAttribute not working

MSDN - EditorFor

MSDN - TextAreaFor



来源:https://stackoverflow.com/questions/50648960/textareafor-not-working-when-using-htmlattributes-overload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!