Asp.net MVC文本区域

32
在Asp.net Mvc中如何调整TextArea的大小并为其分配模型值。
4个回答

34

试试这个:

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

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

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

因为这个原因:

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);

}

对于您的第一个示例:<%= Html.TextAreaFor(m => m.Description, 15, 20, null) %> - Simon Bartlett
无论添加还是删除字典都无所谓,我认为问题的作者可以处理这个。 - Aleksei Anufriev

24
我找到了一个简单的方法来实现这一点。 使用模型注释,Razor会聪明地生成

10

假设你有一个强类型的视图,针对某个模型类,你可以使用以下代码:

<%= Html.TextAreaFor(x => x.SomeProperty, new { rows = "20", cols = "10" }) %>
或者:
<%= Html.TextAreaFor(x => x.SomeProperty, 20, 10, new { @class = "foo" }) %>

针对您的第二个示例:<%= Html.TextAreaFor(x => x.SomeProperty, 20, 10, null) %> - Simon Bartlett

1
Pitfall在使用@Html.TextAreaFor时是因为它没有重载,允许您分配模型值。 示例1:
 @Html.TextAreaFor(m => m.Language, 6, 40, new { @class = "form-control",@value="Tft.WebRole.Properties.Settings.Default.DefaultLanguage"}

例子1不会引发异常,也不会显示任何文本。请将其放下。

解决方法:

使用@Html.TextArea代替。

例子2:

@Html.TextArea("Language", Tft.WebRole.Properties.Settings.Default.DefaultLanguage, 6, 40, new { @class = "form-control" })

建议:

你应该也放弃使用Aspx,因为Razor更轻量且语法等效。

只需使用@代替<%= %>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接