Search code examples
c#asp.netasp.net-mvc-4html-helper

keep getting "cannot be inferred" error when trying to call my htmlhelper


I am using Visual Studio Express 2013 for Web and Framework 4.5. My Index.view calls this partial view:

@Html.Partial("_MyCategories", Model)

my model is referenced in the partial view:

@using MyApp.ModelHelpers
@model MyApp.Models.MyViewModel

In the partial view I am trying to call htmlHelpers class inside a loop in the model:

@Html.SpecialTextBoxFor(Model.Category[i].Name)

and in the htmlHelpers class I have this method:

public static MvcHtmlString SpecialTextBoxFor<TModel, TProperty>(this HtmlHelper htmlHelper, Expression<Func<TModel, TProperty>> expression)

When I try to call my htmlhelper from the partial view using this:

@Html.SpecialTextBoxFor(Model.Category[i].Name)

I get this error: The type arguments for method 'MyApp.HtmlHelpers.SpecialTextBoxFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

I have tried

@Html.SpecialTextBoxFor(m => m.Category[i].Name)

after the dot, it can't find the Category

in my web.config I have the proper setting:

<compilation debug="true" targetFramework="4.5" />

I tried resetting it to 4.5.1 according to some post, but no change.

I verified that I have the correct reference to my namespace in the views web.config:

<add namespace="MyApp" />

and that I also have in my global.asax.cs

ViewEngines.Engines.Add(new RazorViewEngine());

What else should I try!?


Solution

  • You need to change your declaration of the extension function to:

    public static MvcHtmlString SpecialTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)