Jeremy
Jeremy

Reputation: 46350

MVC view can't find my extension method

I've created an extension method:

namespace MyComp.Web.MVC.Html
{
    public static class LinkExtensions
    {
        public static MvcHtmlString ActionImageLink(this HtmlHelper htmlHelper, string linkText, string imageSource, string actionName)
        {
            ...
        }
    }
}

I've referenced the assembly from my mvc app, and I've tried importing the namespace in my view:

<%@ Import Namespace="MyComp.Web.Mvc.Html" %>

and I've also added it to the web config file:

<pages>
    <controls>
        ...
    </controls>
    <namespaces>
        <add namespace="System.Web.Mvc"/>
        <add namespace="System.Web.Mvc.Ajax"/>
        <add namespace="System.Web.Mvc.Html"/>
        <add namespace="System.Web.Routing"/>
        <add namespace="System.Linq"/>
        <add namespace="System.Collections.Generic"/>
        <add namespace="MyComp.Web.Mvc.Html"/>
    </namespaces> 
</pages>

In My view if I try to access Html.ActionImageLink I get an error saying that System.Web.Mvc.HtmlHelper does not contain a definition for ActionImageLink accepting a first argument type of System.Web.Mvc.HtmlHelper. I don't see any of the ActionLink extension methods for System.Web.Mvc.HtmlHelper, only for System.Web.Mvc.HtmlHelper, so how does it work for the .net framework, and not for me?

Upvotes: 11

Views: 11212

Answers (6)

Shan
Shan

Reputation: 61

Close and reopen Visual Studio did the trick!!

Upvotes: 2

LastCyborg
LastCyborg

Reputation: 124

You must add the namespace in the web.config but in the one inside the Views Folder

Upvotes: 6

hunter
hunter

Reputation: 63522

Try shutting down Visual Studio and opening your Solution again. When things start acting weird, some times this helps.

Upvotes: 4

Anthony Serdyukov
Anthony Serdyukov

Reputation: 4328

Notice the difference in the case of your namespace when declaring and when importing.

namespace MyComp.Web.MVC.Html
{
} 

<%@ Import Namespace="MyComp.Web.Mvc.Html" %>
<add namespace="MyComp.Web.Mvc.Html"/>

Namespaces are case-sensitive!

Upvotes: 14

PanJanek
PanJanek

Reputation: 6675

  1. Does the VS intellisense autocompletes your extension method? Does it autocompletes standard MVC helpers methods? If not then the view complilation error occured. Make sure you have the proper "Inherits" attribute value in Page tag at the beginning of the view. If you use strongly typed views make sure the "strong type" exists and compiles.

  2. Do you define the extension method in the same project where the view is defined? If not you have to add the reference in the mvc project. Finally check if the assembly with the extension method (MyComp.Web.Mvc.Html.dll?) is in the Bin folder of the application

  3. Try to add the namespace declaration to the pages/namespaces section of the web.config file placed in your Views folder in MVC project (not the main project web.config file).

Upvotes: 1

San
San

Reputation: 2346

One of the reasons may be you are returning a MvcHtmlString and not a string. Include the namespace for class MvcHtmlString. See if it helps.

Upvotes: 0

Related Questions