Error in HtmlHelper function in a view. It works fine in a .cs file.

I'm following a tutorial in Pro ASP.NET MVC 5.  I get this error:

Severity      Error

Code            CS1061      
Description      'HtmlHelper<ProductsListViewModel>' does not contain a definition for 'PageLinks'
            and no extension method 'PageLinks' accepting a first argument of type
            'HtmlHelper<ProductsListViewModel>' could be found
            (are you missing a using directive or an assembly reference?)      
Project      File      SportsStore.WebUI      

Line            SportsStore\SportsStore.WebUI\Views\Product\List.cshtml      15

Suppression State      Active

The code is attached.

The error points to line 15 of LIst.cshtml.   It seems to say that I'm passing a ProductsListViewModel as the first argument, but I'm passing a PagingInfo.  I successfully call this method in UnitTest.cs

Why does it think a PagingInfo is it's parent, ProductsListViewModel?
Barry SweezeySoftware EngineerAsked:
Who is Participating?
Okay, try one other thing for me.
The calling code you posted is:

@Html.PageLinks(Model.PagingInfo, i => i);

Open in new window

That could conceivably be causing the problem, because it would be expecting a Func<int, int> instead of the Func<int, string> you have declared.
So might be failing to find a proper overload.

Try changing to
@Html.PageLinks(Model.PagingInfo, i => i.ToString());

Open in new window

The view doesn't have native visibility of the PageHelpers class you created.
Go to the "sub" web.config under the Views folder.  You should have several default namespaces already included in there, add another one to include the SportsStore.WebUI.HtmlHelpers namespace.
Then the view should be able to pick up the extension method.
Barry SweezeySoftware EngineerAuthor Commented:
Thanks for your reply.  I added the line to SportsStore\SportsStore.WebUI\Views\web.config, but the error is the same.

    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="SportsStore.WebUI" />
        <add namespace="SportsStore.WebUI.HtmlHelpers" />
Barry SweezeySoftware EngineerAuthor Commented:
Thanks for your help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.