2010-12-17 91 views

回答

33

试试这个:

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

} 
+0

对于你的第一个例子:<%= Html.TextAreaFor(m => m.Description,15,20,null)%> – 2010-12-17 09:56:20

+0

它没关系添加或删除字典=)我认为问题作者可以处理这个= ) – 2010-12-17 10:00:38

9

假设你有一个强类型的视图一些模型类,你可以使用如下:

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

或:

<%= Html.TextAreaFor(x => x.SomeProperty, 20, 10, new { @class = "foo" }) %> 
+0

对于你的第二示例:<(%)= Html.TextAreaFor(X => x.SomeProperty,20,10,NULL) %> – 2010-12-17 09:55:20

1

陷阱@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的太多,因为剃须刀是更轻和等效的语法。

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

22

我发现了一个简单的客场实现这一目标。

使用模型注释剃须刀将足够智能生成textarea

型号:

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

检视:

@Html.EditorFor(model => model.Comments)