ASP.NET MVC @Html.EditorFor TextArea

@Html.EditorFor creates single line text box for string data by default. That is good for short fields like user name or article title, but not comfortable for longer multi line content.

For long fields, using of textarea tag is more suitable solution. To inform Razor to use multi line textarea instead of single line text box, add this attribute to your model property:

When you use DataType.MultilineText parameter, Razor will render textarea tag instead of single-line text box. That’s good enough in many cases, but sometimes you want more control over width, height and other styles of textarea. You can change text area styles with CSS:

This CSS will adjust all textarea tags to same styles. If you want different styles for text fields, define CSS class in view in EditorFor method like this:

Another option is to use @Html.TextAreaFor instead of @Html.EditorFor. Advantage of this method is that you don’t need to change model class. That could be preferred option if you work with automatically generated model, like when Entity Framework database first is used. Code in view could look like this:

Please notice there are 4 parameters used for TextAreaFor method. Second and third parameters define rows and columns of textarea. Sample above will create text area of 10 rows and 80 columns. So, now you have textarea without need to change model.

Happy coding!

Share on FacebookShare on LinkedInEmail this to someoneTweet about this on TwitterShare on Google+
  • Paris Pantigoso

    Thank you for your tips!