Creating a multi-line text field using the Html.Helper function

I am trying to create a multi-line text file using ASP.NET MVC with the following code.

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

It displays only a single line text block with a fixed size.

on the other hand

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

displays the correct view, but in the post post method named formCollection named form

 form["Body"]; 

returns a null value.

+46
c # asp.net-mvc
Feb 16 '11 at 20:50
source share
6 answers

Multi-line text field in html <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 another possibility is to decorate your view model property with the [DataType] attribute:

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

and in your opinion:

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

and set the width and height using CSS.

+97
Feb 16 '11 at 20:56
source share

MVC4 you should use:

 @Html.TextAreaFor(x => x.Body, 10, 15, null) 
+6
Nov 22
source share

This allows multi-line, set custom width, height and installation location. To check, use StringLength or RegularExpression in Model.cs

Razor View Syntax

 @Html.TextAreaFor(model => model.property, new { style = "width: 420px; height: 100px;", placeholder = "Placeholder here.." }) 
+1
Nov 10 '15 at 8:54
source share

I think Html.EditorFor is what you are looking for. This is only for MVC2 and up, though. Does it help?

If you use DataAnnotations and decorate your property with [DataType(DataType.MultilineText)] MVC attribute should set the required html for you.

0
Feb 16 '11 at 20:58
source share

In the Entity layer:

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

And in sight:

 @Html.TextAreaFor(model => model.Body, new { rows = 10, cols = 50 }) 
0
Nov 22 '15 at 15:20
source share

VB.net Solution:

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

0
Jan 12 '16 at 16:15
source share



All Articles