53

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.

2
  • Just noticed that on your first example, you have Columns = "55px". Is that a typo or it's like that in the code? See if taking that out fixes your problem :) I am not sure if it will so that's why this is just a comment... Commented Feb 16, 2011 at 21:04
  • I would still go down the dataannotations route, unless this is an MVC1 project Commented Feb 16, 2011 at 21:07

6 Answers 6

108

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I usually use EditorFor, and set height etc with Css. Seems cleaner
Thanks a lot, that works! I also just figured out that the attribute for columns is cols...
@Html.TextAreaFor(model => model.Comments, 5, 2, null) worked for the rows but the columns appears to be ignored?
with class like new { @class = "form-control" } check here
6

MVC4 you should use:

@Html.TextAreaFor(x => x.Body, 10, 15, null)

1 Comment

with class like new { @class = "form-control" } check here
4

This allows to multi-line, set custom width and height and setting place holder. For validation used StringLength or RegularExpression in Model.cs

Razor View Syntax

@Html.TextAreaFor(model => model.property, new { style = "width: 420px; height: 100px;", placeholder = "Placeholder here.." })

Comments

0

I think the Html.EditorFor Is what you're looking for. That's only for MVC2 and up though. Does that help?

If you're using DataAnnotations and decorate your property with the [DataType(DataType.MultilineText)] Attribute, MVC should scaffold out the required html for you.

1 Comment

There is no MultilineText attribute. Do you mean DataType attribute?
0

In Entity layer:

[MaxLength(500)]
public string Body { get; set; }

And in view:

@Html.TextAreaFor(model => model.Body, new { rows = 10, cols = 50 })

Comments

0

VB.net solution:

@Html.TextAreaFor(Function(Model) Model.Body, 3, 55, Nothing)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.