使用Html.Helper函数创build多行文本框

我想用下面的代码创build一个使用ASP.NET MVC的多行文本框。

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

它只显示一行固定大小的文本框。

另一方面

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

呈现正确的视图,但是在控制器的post方法中用formCollection命名forms

 form["Body"]; 

返回一个空值。

html中的多行文本框是<textarea>

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

要么:

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

甚至更好:

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

而另一种可能性是用[DataType]属性来修饰你的视图模型属性:

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

并在你看来:

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

并通过CSS设置宽度和高度。

你应该使用MVC4:

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

这允许多行,设置自定义的宽度和高度,并设置占位符。 对于使用Model.cs中的StringLength或RegularExpression进行validation

剃刀视图语法

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

我认为Html.EditorFor是你在找什么。 这只是MVC2和虽然。 这有帮助吗?

如果您使用DataAnnotations并使用[DataType(DataType.MultilineText)]属性装饰您的属性,那么MVC应该为您提供必要的html脚本。

在实体层:

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

而且鉴于:

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

VB.net解决scheme:

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