C# – creating multiline textbox using Html.Helper function

asp.netasp.net-mvcc

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.

Best Answer

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.