How do I specify the columns and rows of a multiline Editor-For in ASP.MVC?

Dan Sorensen picture Dan Sorensen · Jun 2, 2011 · Viewed 102.6k times · Source

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?

Answer

amit_g picture amit_g · Jun 2, 2011

Use TextAreaFor

@Html.TextAreaFor(model => model.Description, new { @class = "whatever-class", @cols = 80, @rows = 10 })

or use style for multi-line class.

You could also write EditorTemplate for this.