Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 896
Jonathan Allen Avatar asked Jul 01 '11 20:07

Jonathan Allen


People also ask

How to add multiline textbox in mvc?

Enter or fill the textbox with multiple rows of text. Choose the corresponding option from the property panel to update the multiline textbox. The Multiline Textbox is used to edit or display multiple lines of text that helps you to accept address, description, comments, feedbacks, and more in a form.

What is the difference between HTML textbox HTML TextBoxFor and HTML EditorFor?

Show activity on this post. TextBoxFor: It will render like text input html element corresponding to specified expression. In simple word it will always render like an input textbox irrespective datatype of the property which is getting bind with the control. EditorFor: This control is bit smart.

What is HTML EditorFor in MVC?

ASP.NET MVC includes the method that generates HTML input elements based on the datatype. The Html. Editor() or Html. EditorFor() extension methods generate HTML elements based on the data type of the model object's property.

How to display multiline text in a label control in asp net?

You can concatenate the string in asp:label with "<br/>" because it will result in html. If you don't specific the width of label, it will auto expand the width to fit your string.


2 Answers

Use data type 'MultilineText':

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

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

like image 127
Rajesh Avatar answered Oct 23 '22 03:10

Rajesh


in your view, instead of:

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

just use:

@Html.TextAreaFor(model => model.Comments[0].Comment, 5, 1, null) 
like image 21
Fabio Marreco Avatar answered Oct 23 '22 03:10

Fabio Marreco