Rendering a <textarea /> using data annotations

14,405

Solution 1

Why not use:

 @Html.TextAreaFor(model => model.YourProperty)

EditorFor helper is sort of "smart" helper and it's basing the rendering based on the underlying type of the property. If you want to enforce it to render a specific html input type then use other helpers.

Solution 2

[DataType(DataType.MultilineText)] only works if you use Html.EditorFor helper in your view:

Sample:

Model

[DataType(DataType.MultilineText)]
public string Description { get; set; }

View

Html.EditorFor(m => m.Description)
Share:
14,405
serlingpa
Author by

serlingpa

Updated on June 05, 2022

Comments

  • serlingpa
    serlingpa almost 2 years

    I'd like to render a text area for one of the fields in my model.

    I've tried applying [DataType(DataType.MultilineText)] to my field, but this doesn't do the trick.

    At the moment, I am rendering the text area manually but I'd much prefer to use Html.EditorFor. Any suggestions?

  • serlingpa
    serlingpa almost 11 years
    Oh yeah, never thought of that. Thanks!
  • Codeman
    Codeman over 9 years
    This works great when you're being lazy, my favorite!
  • niico
    niico over 7 years
    I think this is a better, more reusable answer.