0

I did this custom helper:

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

To avoid need to add class attributes to field of specific type.

But. When using this helper:

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

I received this error:

CS7036: There is no argument given that corresponds to the required formal parameter 'expression' of 'RzHelpers.RzTextBoxForCNPJ(HtmlHelper, Expression>)'

As if I was passed wrong type or number of parameters.

It is not my first helper and never must pass the first parameter, only from the 2nd onwards.

What am I forgotting?

Neumann
  • 319
  • 2
  • 14

1 Answers1

1

You are calling this method as a static method of RzHelpers class. So the method is not used as an extension and requires the first parameter. Hence the error.

You should be calling it as a method of an instance you are extending, HtmlHelper:

@Html.RzTextBoxForCNPJ(model => model.Cnpj)
Andrei
  • 55,890
  • 9
  • 87
  • 108