Как задать размер поля textarea в ASP.NET MVC? - CodeHelper

Как задать размер поля textarea в ASP.NET MVC?

1

Для генерации поля textfield используются дополнительные html-атрибуты cols и rows:

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

Но в итоговой разметке все равно создается texarea другого размера:

<textarea rows="2" name="Description" id="Description" cols="20"></textarea>

В чем здесь может быть подвох?

Лучший ответ:

1

Если посмотреть реализацию метода TextAreaExtensions.TextArea(), то видно, что в нем значения атрибутов установки размерности перекрываются значениями по умолчанию:

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

Поэтому указывать rows и cols в атрибутах бесполезно. Нужно использовать один из методов:

public static string TextArea(
    this HtmlHelper htmlHelper, string name, string value, 
    int rows, int columns, 
    IDictionary<string, object> htmlAttributes);

или

public static string TextAreaFor<TModel, TProperty>(
    this HtmlHelper<TModel> htmlHelper, 
    Expression<Func<TModel, TProperty>> expression, 
    int rows, int columns, 
    IDictionary<string, object> htmlAttributes)

Оба метода для определения дополнительных атрибутов принимают словарь, а не анонимный объект, поэтому придется писать что-то типа этого:

<%=Html.TextAreaFor(
    m => m.Description, 15, 20, 
    new RouteValueDictionary(new { @class = "someClass"}))%>
v1.7.123.556
© 2009—2010 CodeHelper FAQ | О сайте | Обратная связь | История изменений | Статьи
ms tax id ms tax id Creative Commons LicenseМатериалы сайта распространяются под лицензией Creative Commons Attribution-Share Alike 3.0 Unported.