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.
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.
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.
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.
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.
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.
MVC4 you should use:
@Html.TextAreaFor(x => x.Body, 10, 15, null)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With