在Asp.net Mvc中如何调整TextArea的大小并为其分配模型值。
试试这个:
<%=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(x => x.SomeProperty, new { rows = "20", cols = "10" }) %>
或者:<%= Html.TextAreaFor(x => x.SomeProperty, 20, 10, new { @class = "foo" }) %>
@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更轻量且语法等效。
只需使用@
代替<%= %>
。