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

时间:2011-02-16 20:50:35

标签: c# asp.net asp.net-mvc

我正在尝试使用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命名为form

form["Body"]; 

返回空值。

6 个答案:

答案 0 :(得分:100)

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设置宽度和高度。

答案 1 :(得分:6)

你应该使用MVC4:

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

答案 2 :(得分:3)

这允许多行,设置自定义宽度和高度以及设置占位符。 对于验证,在Model.cs中使用了StringLength或RegularExpression

Razor查看语法

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

答案 3 :(得分:0)

我认为Html.EditorFor正是您所寻找的。这只适用于MVC2及以上版本。这有帮助吗?

如果您正在使用DataAnnotations并使用[DataType(DataType.MultilineText)]装饰您的媒体资源  属性,MVC应为你支持所需的html。

答案 4 :(得分:0)

在实体层中:

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

并且在视野中:

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

答案 5 :(得分:0)

VB.net解决方案:

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

相关问题