0

我知道如何扩展 TextBoxFor :

public static MvcHtmlString TextBoxFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
        {
            MvcHtmlString html = default(MvcHtmlString);
            RouteValueDictionary routeValues = new RouteValueDictionary(htmlAttributes);         
            html = System.Web.Mvc.Html.InputExtensions.TextBoxFor(htmlHelper, expression, routeValues);
            return html;
        }

我想为 TextAreaFor 做同样的事情,但不幸的是System.Web.Mvc.Html.InputExtensions不包含 TextAreaFor 方法。我该如何解决这个问题?

4

2 回答 2

2

文档,它在TextAreaExtensions静态类中

return System.Web.Mvc.Html.TextAreaExtensions.TextAreaFor(htmlHelper, expression, routeValues);

要不就

return htmlHelper.TextAreaFor(expression, routeValues);

顺便说一句,第三个参数只是(如在 TextBoxFor 中) a IDictionary<string, Object> htmlAttributes,与 RouteValues 无关。

它只是工作,因为RouteValueDictionaryimplements IDictionary<string, Object>,但你的论点令人困惑。

于 2013-08-26T16:24:08.157 回答
1

如果您标记您的属性[System.ComponentModel.DataAnnotations.DataType(DataType.MultilineText)]然后使用EditorFor您应该得到您想要的结果。

public class SomeClass
{
    [DataType(DataType.MultilineText)]
    public string Name { get; set; }
}

@Html.EditorFor(x => x.Name)
于 2013-08-26T16:27:53.877 回答