In ASP.MVC 3, how do I specify the columns and rows for a multiline EditorFor
(textarea)? I am using [UIHint("MultilineText")]
, but can't find any documentation on how to add attributes for the text area.
Desired HTML:
<textarea cols="40" rows="10"></textarea>
Relevant Part of my MVC 3 Model:
[UIHint("MultilineText")]
public string Description { get; set; }
Relevant Part of my Razor cshtml:
<div class="editor-field">
@Html.EditorFor(model => model.Description)
</div>
What I'm getting in View Source:
<div class="editor-field">
<textarea class="text-box multi-line" id="Description" name="Description"></textarea>
</div>
How do I set rows and columns?
One option seems to be using CSS to style the textarea
See this entry on SO or this one.
Amurra's accepted answer seems to imply this class is added automatically when using EditorFor but you'd have to verify this.
EDIT: Confirmed, it does. So yes, if you want to use EditorFor, using this CSS style does what you're looking for.
In .net VB - you could achieve control over columns and rows with the following in your razor file:
Use TextAreaFor
or use style for
multi-line
class.You could also write EditorTemplate for this.
In ASP.NET MVC 5 you could use the
[DataType(DataType.MultilineText)]
attribute. It will render a TextArea tag.Then in the view if you need to specify the rows you can do it like this:
Or just use the TextAreaFor with the right overload:
This one can also be used with less effort I believe (but I am in MVC 5)
in mvc 5