55

This is my code:

    <div class="editor-label">
        @Html.LabelFor(model => model.Comments[0].Comment)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Comments[0].Comment)
        @Html.ValidationMessageFor(model => model.Comments[0].Comment)
    </div>

This is what it generates:

    <div class="editor-label">
        <label for="Comments_0__Comment">Comment</label>
    </div>
    <div class="editor-field">
        <input class="text-box single-line" data-val="true" data-val-required="The Comment field is required." id="Comments_0__Comment" name="Comments[0].Comment" type="text" value="" />
        <span class="field-validation-valid" data-valmsg-for="Comments[0].Comment" data-valmsg-replace="true"></span>
    </div>

How do I tell it that the field should be a text box with five lines instead of just a single-line text box?

Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447
  • 1
    possible duplicate of [ASP.NET MVC3 - textarea with @Html.EditorFor](http://stackoverflow.com/questions/4927003/asp-net-mvc3-textarea-with-html-editorfor) – Brad Rem Oct 24 '13 at 18:02

3 Answers3

129

Use data type 'MultilineText':

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

See ASP.NET MVC3 - textarea with @Html.EditorFor

Community
  • 1
  • 1
Rajesh
  • 2,472
  • 3
  • 25
  • 31
47

in your view, instead of:

@Html.EditorFor(model => model.Comments[0].Comment)

just use:

@Html.TextAreaFor(model => model.Comments[0].Comment, 5, 1, null)
Fabio Marreco
  • 2,186
  • 2
  • 23
  • 24
  • 8
    The nice thing that I do like about this approach is that I do not need to be going all the way into my properties in another assembly .. my model classes of properties ( sure viewmodels should be used more, but it is situational depending on the application etc.. ) Plus the selected answer the problem with that is what if another page or application wants the text to not be multiline. IMO this is great for some applicaitons while the other is fine for others. For me it was @Html.TextAreaFor(model => model.Comments, 5, 1, new { @class = "form-control" }) thx – Tom Stickel Sep 01 '15 at 21:56
13

Another way

@Html.TextAreaFor(model => model.Comments[0].Comment)

And in your css do this

textarea
{
    font-family: inherit;
    width: 650px;
    height: 65px;
}

That DataType dealie allows carriage returns in the data, not everybody likes those.

SomeGuy
  • 131
  • 1
  • 2