Asp.net MVC TextArea

yog*_*gee 31 asp.net-mvc

如何调整TextArea的大小并在Asp.net Mvc中为其指定模型值

Ale*_*iev 34

试试这个:

 <%=Html.TextAreaFor(
        m => m.Description, 15, 20, 
        new RouteValueDictionary(new { @class = "someClass"}))%>
Run Code Online (Sandbox Code Playgroud)

编辑:
据我所知,这不会起作用

<%=Html.TextAreaFor(m => m.Description, new { cols = "20", rows = "15" })%>
Run Code Online (Sandbox Code Playgroud)

因为这:

private const int TextAreaRows = 2;
private const int TextAreaColumns = 20;

// ...





     public static string TextArea(
                this HtmlHelper htmlHelper, string name, 
                IDictionary<string, object> htmlAttributes) {
            Dictionary<string, object> implicitAttributes = new Dictionary<string, object>();
            implicitAttributes.Add("rows", TextAreaRows.ToString(CultureInfo.InvariantCulture));
            implicitAttributes.Add("cols", TextAreaColumns.ToString(CultureInfo.InvariantCulture));
            return TextAreaHelper(htmlHelper, name, true /* useViewData */, null /* value */, implicitAttributes, null /* explicitParameters */, htmlAttributes);

}
Run Code Online (Sandbox Code Playgroud)


med*_*eda 24

我找到了一个简单的方法来实现这一点.

使用模型注释剃刀将足够聪明地生成textarea.

模型:

[DataType(DataType.MultilineText)]
public string Comments { get; set; }
Run Code Online (Sandbox Code Playgroud)

视图:

@Html.EditorFor(model => model.Comments)
Run Code Online (Sandbox Code Playgroud)


Dar*_*rov 9

假设您对某个模型类具有强类型视图,则可以使用以下命令:

<%= Html.TextAreaFor(x => x.SomeProperty, new { rows = "20", cols = "10" }) %>
Run Code Online (Sandbox Code Playgroud)

要么:

<%= Html.TextAreaFor(x => x.SomeProperty, 20, 10, new { @class = "foo" }) %>
Run Code Online (Sandbox Code Playgroud)