2010-12-17 16 views

Respuesta

33

Prueba esto:

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

Editar:
Esto no funcionará por lo que yo sé

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

debido a esto:

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

Para su primer ejemplo: <% = Html.TextAreaFor (m => m.Description, 15, 20, null)%> –

+0

no importa agregar o quitar el diccionario =) Creo que el autor de la pregunta puede manejar esto =) –

9

Asumiendo que tiene un punto de vista inflexible a alguna clase de modelo se puede utilizar el siguiente:

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

o:

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

Para su segundo ejemplo: <% = Html.TextAreaFor (x => x.SomeProperty, 20, 10, null) %> –

1

trampa es @Html.TextAreaFor porque no tiene la sobrecarga que le permiten asignar un valor Modelo.

Ejemplo 1:

@Html.TextAreaFor(m => m.Language, 6, 40, new { @class = "form-control",@value="Tft.WebRole.Properties.Settings.Default.DefaultLanguage"} 

Ejemplo 1 elevar costumbre excepción y suele mostrar cualquier texto. Decepcionado.

Solución:

uso @Html.TextArea lugar

Ejemplo 2:

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

Consejo:

Debe defraudado Aspx también porque la maquinilla de afeitar es una sintaxis más ligera y equivalente.

sólo tiene que utilizar @ en lugar de <%= %>.

22

me encontré con un simple distancia para lograr esto.

El uso de maquinilla de anotación de modelos será lo suficientemente inteligente como para generar el textarea.

Modelo:

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

Vista:

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