Subtle difference between Html.TextAreaFor and Html.EditorFor

Hello everybody,

today I want to document one gotcha related to difference between Html.TextAreaFor and Html.EditorFor.

Recently I've had a requirement to add hint text to text fields. In Html world there are two ways to create input field which accepts text: <input type="text" /> and <textarea>some text </textarea>. In MVC world you can create those tags with help of Html.TextAreaFor and Html.EditorFor.

I've had challenges with trying to apply attribute placeholder to TextAreaFor. EditorFor was pretty simplte to google and find:

@Html.EditorFor(model => model.YourName, new { htmlAttributes = new { @class = "form-control", placeholder="Your name" } })

But finally after a research I've found that for TextAreaFor I should use direct Key/Value binding. Like this:

@Html.TextAreaFor(model => model.Message, new { placeholder = "Message",  @class = "form-control"})

Take note that with TextAreaFor I didn't use htmlAttributes at all. 

No Comments

Add a Comment