75

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?

user247702
  • 23,641
  • 15
  • 110
  • 157
Dan Sorensen
  • 11,403
  • 19
  • 67
  • 100

7 Answers7

148

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.

amit_g
  • 30,880
  • 8
  • 61
  • 118
  • For anyone reading this: the `@` at `@cols` and `@rows` are unnecessary. I.e. `cols` and `rows` are sufficient. – Uwe Keim Jul 18 '21 at 12:52
30

In ASP.NET MVC 5 you could use the [DataType(DataType.MultilineText)] attribute. It will render a TextArea tag.

public class MyModel
{
    [DataType(DataType.MultilineText)]
    public string MyField { get; set; }
}

Then in the view if you need to specify the rows you can do it like this:

@Html.EditorFor(model => model.MyField, new { htmlAttributes = new { rows = 10 } })

Or just use the TextAreaFor with the right overload:

@Html.TextAreaFor(model => model.MyField, 10, 20, null)
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
Juan Carlos Puerto
  • 2,632
  • 1
  • 26
  • 22
9

This one can also be used with less effort I believe (but I am in MVC 5)

@Html.Description(model => model.Story, 20, 50, new { })

enter image description here

5

One option seems to be using CSS to style the textarea

.multi-line { height:5em; width:5em; }

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.

<textarea class="text-box multi-line" id="StoreSearchCriteria_Location" name="StoreSearchCriteria.Location">
Community
  • 1
  • 1
Khepri
  • 9,547
  • 5
  • 45
  • 61
  • Thank you for answering the original poster's question. When using a framework that creates input fields using `EditorFor`, e.g _Telerik ASP.NET MVC_, substituting `TextAreaFor` is not an option. Thanks for this. – Suncat2000 Jul 15 '15 at 13:18
1

in mvc 5

              @Html.EditorFor(x => x.Address, 
              new {htmlAttributes = new {@class = "form-control", 
                   @placeholder = "Complete Address", @cols = 10, @rows = 10 } })
CyberNinja
  • 872
  • 9
  • 25
0

In .net VB - you could achieve control over columns and rows with the following in your razor file:

@Html.EditorFor(Function(model) model.generalNotes, New With {.htmlAttributes = New With {.class = "someClassIfYouWant", .rows = 5,.cols=6}})
Jason Roman
  • 8,146
  • 10
  • 35
  • 40
0

@Html.TextArea("txtNotes", "", 4, 0, new { @class = "k-textbox", style = "width: 100%; height: 100%;" })

Burhan Kaya
  • 139
  • 2
  • 3