【问题标题】:Parameters to helpers助手参数
【发布时间】:2018-03-23 16:09:19
【问题描述】:

我做了这个自定义助手:

 public static System.Web.Mvc.MvcHtmlString RzTextBoxForCNPJ<TModel, TValue>(this System.Web.Mvc.HtmlHelper<TModel> html,
            System.Linq.Expressions.Expression<System.Func<TModel, TValue>> expression)
        {
            System.Collections.Generic.Dictionary<string, object> htmlAttributes = new System.Collections.Generic.Dictionary<string, object>();

            htmlAttributes.Add("class", "form-control cnpj");

            return (html.TextBoxFor(expression, htmlAttributes));
        }

为了避免需要将类属性添加到特定类型的字段。

但是。使用此助手时:

@RzHelpers.RzTextBoxForCNPJ(model => model.Cnpj)

我收到了这个错误:

CS7036:没有给出与“RzHelpers.RzTextBoxForCNPJ(HtmlHelper, Expression>)”所需的形参“表达式”相对应的参数

好像我传递了错误的类型或数量的参数。

它不是我的第一个助手,并且永远不必传递第一个参数,只能从第二个开始。

我忘记了什么?

【问题讨论】:

    标签: asp.net-mvc html-helper


    【解决方案1】:

    您将此方法作为RzHelpers 类的静态方法调用。所以该方法不作为扩展使用,需要第一个参数。因此出现错误。

    您应该将其作为您正在扩展的实例的方法调用,HtmlHelper:

    @Html.RzTextBoxForCNPJ(model => model.Cnpj)
    

    【讨论】:

    • 太棒了。工作。
    猜你喜欢
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 2013-05-13
    • 2013-08-14
    • 2014-01-08
    • 2015-10-10
    相关资源
    最近更新 更多