【问题标题】:Why is TextAreaFor helper's rows property not settings the html rows attribute properly?为什么 TextAreaFor 助手的 rows 属性没有正确设置 html rows 属性?
【发布时间】:2013-12-18 16:40:32
【问题描述】:

当我在 MVC 中使用 TextAreaFor 助手并定义如下行时:

@Html.TextAreaFor(model => model.Notes, 5)

此文本区域的行数未设置,这意味着文本区域的大小完全不受影响。但是,如果我使用 htmlAttributes 来定义行:

@Html.TextAreaFor(model => model.Notes, new { rows = 5 })

然后它工作正常。所以我的问题是,如果不定义 textarea 的 rows HTML 属性,TextAreaFor 助手中的 rows 属性是什么?我在这里错过了什么?

【问题讨论】:

    标签: .net asp.net-mvc html-helper razor-2


    【解决方案1】:

    没有像您的第一个示例那样的重载。你可以试试this重载

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

    所以你的例子应该是这样的

    @Html.TextAreaFor(model => model.Notes,5,10, new { @class= "myCssClassName" })
    

    对于第二个示例,您以正确的方式使用 the overload

    【讨论】:

    • 所以你是说我必须定义所有这些其他参数 - 它们不是可选的?
    • 您可以简单地使用第二种方法(仅提供 html 属性)。您可以根据您的要求使用不同的重载。在这种情况下,如果您只想指定行和列,只需使用上述任何一种即可。
    • @RaphaëlAlthaus 这就是他在第二个示例中使用的内容,并且效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    • 2015-03-17
    • 2011-07-27
    相关资源
    最近更新 更多