1

I have a text box that I want to expand and add a few lines to make it a text area instead of a small box.

original code

 @Html.EditorFor(x => x.emailBody)

<input type="submit" value="Send Mail" class="btn btn-default" />

I have found this on SOF, but it is not working and the field looks the same

@Html.EditorFor(x => x.emailBody, new { @class = "form-control", @rows = 5 })
<input type="submit" value="Send Mail" class="btn btn-default" />

Can anyone suggest a simple solution

3 Answers3

1

Add a DataType.MultilineText attribute to the property and @Html.EditorFor() will render a <textarea> instead of <input type="text" ../>

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

Note also (assuming your using MVC-5.1+), then to add html attributes, it should be

@Html.EditorFor(x => x.emailBody, new { htmlAttributes = new { @class = "form-control", @rows = 5 } })

If you have only MVC-5, then use @Html.TextAreaFor() or create a custom EditorTemplate and pass the attributes as AdditionalViewData (example here)

Community
  • 1
  • 1
  • Thanks 4 a fast solution :-) – Robin John Muirhead Jan 16 '15 at 11:54
  • Note also (and I'm assuming your using MVC-5.1+) then to add the html attributes, it should be `@Html.EditorFor(x => x.emailBody, new { htmlAttributes = new { @class = "form-control", @rows = 5 } })` otherwise you have to use `@Html.TextAreaFor()` or create a custom `EditorTemplate` –  Jan 16 '15 at 12:02
1
@Html.TextAreaFor(model => model.emailBody, new { @rows = 3 })

you can use textarea for and set rows according to your choice its better way then increasing line of a normal textbox....

shakil ahmad
  • 145
  • 1
  • 11
0

Why cant you use something like this

@Html.TextAreaFor(model => model.emailBody, new { @rows = 3 })

instead of EditorFor, you can use TextAreaFor to get multiple lines in a textbox

Aashiq
  • 1
  • 1