Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating multiline textbox using Html.Helper function

I am trying to create a multiline Textbox using ASP.NET MVC with the following code.

<%= Html.TextBox("Body", null, new { TextBoxMode = "MultiLine", Columns = "55px", Rows = "10px" })%> 

It just shows up a single line fixed sized textbox.

on the other hand

<asp:TextBox runat="server" ID="Body" TextMode="MultiLine" Columns="55" Rows="10"></asp:TextBox>  

renders the right view, but in the controller's post method with formCollection named form

form["Body"];  

returns a null value.

like image 260
paaone Avatar asked Feb 16 '11 20:02

paaone


People also ask

How will you create multiline text box?

To create a multi-line text input, use the HTML <textarea> tag. You can set the size of a text area using the cols and rows attributes. It is used within a form, to allow users to input text over multiple rows.

What is multiline in TextBox?

A multiline text box control is a large text box that can display several lines of text or accept this text from user input. Text boxes are often linked to select value lookups and detailed menus. You can place a multiline text box control within a section control.

Which helper method renders the text area?

The HtmlHelper class includes two extension methods to render multi-line <textarea> HTML control in a razor view: TextArea() and TextAreaFor<TModel, TProperty>() . By default, it creates a textarea with rows=2 and cols=20.

What is the use of multiline property of TextBox control?

A multiline text box allows you to display more than one line of text in the control. If the WordWrap property is set to true , text entered into the multiline text box is wrapped to the next line in the control.


2 Answers

A multiline textbox in html is <textarea>:

<%= Html.TextArea("Body", null, new { cols = "55", rows = "10" }) %> 

or:

<%= Html.TextArea("Body", null, 10, 55, null) %> 

or even better:

<%= Html.TextAreaFor(x => x.Body, 10, 55, null) %> 

And yet another possibility is to decorate your view model property with the [DataType] attribute:

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

and in your view:

<%= Html.EditorFor(x => x.Body) %> 

and set the width and height through CSS.

like image 85
Darin Dimitrov Avatar answered Sep 28 '22 05:09

Darin Dimitrov


MVC4 you should use:

@Html.TextAreaFor(x => x.Body, 10, 15, null) 
like image 42
ahaliav fox Avatar answered Sep 28 '22 06:09

ahaliav fox