ASP.NET MVC: Html.EditorFor and multi-line text boxes

asp.net Mvc-3Razor

asp.net Mvc-3 Problem Overview


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?

asp.net Mvc-3 Solutions


Solution 1 - asp.net Mvc-3

Use data type 'MultilineText':

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

See https://stackoverflow.com/questions/4927003/asp-net-mvc3-textarea-with-html-editorfor

Solution 2 - asp.net Mvc-3

in your view, instead of:

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

just use:

@Html.TextAreaFor(model => model.Comments[0].Comment, 5, 1, null)

Solution 3 - asp.net Mvc-3

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonathan AllenView Question on Stackoverflow
Solution 1 - asp.net Mvc-3RajeshView Answer on Stackoverflow
Solution 2 - asp.net Mvc-3Fabio MarrecoView Answer on Stackoverflow
Solution 3 - asp.net Mvc-3SomeGuyView Answer on Stackoverflow