2

So, for some hours now, I have been trying to do something that I thought - and still think - should be trivial. Basically, I created a Html helper that I needed to use to apply some CSS attribute to the selected menu of an ASP.NET MVC 3 Application. Here is my Html helper:

namespace MVCUI.Extensibility
{
    public static class HtmlHelpers
    {
        public static MvcHtmlString MenuLink(
            this HtmlHelper helper, 
            string text, 
            string action, 
            string controller, 
            string selectedCssClass, 
            object routeValues, 
            object htmlAttributes)
        {
            var attributes = new RouteValueDictionary(htmlAttributes);

            if (!String.IsNullOrWhiteSpace(selectedCssClass))
            {
                var contextController = helper.ViewContext.RouteData.Values["controller"] as String ?? "Home";
                var contextAction = helper.ViewContext.RouteData.Values["action"] as String ?? "Index";

                if (String.Compare(
                    String.Format("{0}/{1}", controller, action), 
                    String.Format("{0}/{1}", contextController, contextAction), true) == 0)
                {
                    var cssValue = String.Empty;
                    if (attributes.ContainsKey("class"))
                        cssValue = attributes["class"] as String ?? String.Empty;
                    cssValue = cssValue.Trim();
                    if (cssValue.Length > 0)
                        cssValue = cssValue += (" " + selectedCssClass);
                    else
                        cssValue = selectedCssClass;
                    attributes["class"] = cssValue;
                }
            }

            return helper.ActionLink(text, action, controller, new RouteValueDictionary(routeValues), attributes);
        }
    }
}

Here is how I am using it from a _Layout.cshtml file:

@Html.MenuLink("Posts", "Posts", "Post", "selected", new { }, new { })

For some really odd reason, I keep getting the error:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1061: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'MenuLink' and no extension method 'MenuLink' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

Here is what I have tried:

  1. Added <add namespace="MVCUI.Extensibility" /> to <system.web.webPages.razor>/<pages>/<namespaces> section of the Web.config file at the root of the Views folder.
  2. Added @using MVCUI.Extensibility; at the top of the _Layout.cshtml file.
  3. Tried all combinations of (1) and (2) above.
  4. Tried the syntax: @{ Html.MenuLink("Posts", "Posts", "Post", "selected", new { }, new { }); }
  5. Googled, alot! All materials and resources, including our very own stackoverflow, seem to suggest that am doing the right thing.
  6. Even tried setting [assembly: ComVisible(true)] in AssemblyInfo.cs. Well, just in case! ;)

Disclaimer: This is the first time I am trying out a html helper in an ASP.NET MVC 3 application. Where could I be going wrong? Thanks people.

7
  • What do you see in IntelliSense if you type HtmlHelpers. in a view? Commented Oct 23, 2012 at 18:25
  • Can you call it with @HtmlHelpers.MenuLink(Html, "Posts", "Posts", "Post", "selected", new { }, new { })? Or can you call it fully qualified with it's namespace? Commented Oct 23, 2012 at 18:28
  • I can see, Equals, MenuLink and ReferenceEquals methods Commented Oct 23, 2012 at 18:29
  • @Charlino: I tried what you suggested. Including qualifying HtmlHelpers class with the namespace. Its even wierder since it complains that HtmlHelpers is not in MVCUI.Extensibility namespace Commented Oct 23, 2012 at 18:39
  • @JohnGathogo weird... where is the file located? It's definitely getting compiled? What happens if you create another separate helper method that's simple simple simple. Try creating it in another namespace and another directory... basically try and find out what could be wrong with your current setup. Commented Oct 23, 2012 at 19:03

2 Answers 2

1

Like someone pointed out in the comments, there was something different about my setup. In my solution, I had changed the Output path for my projects - including the MVC project - from the default bin\ to something like ..\Library\Build\. No crime there since that setup has worked fine so far.

But, that is what got me into trouble. After I restored the default output path and rebuilt my project the Html helper worked. It continued to work even after I restored back my preferred output path - obviously because the dll in the bin folder got updated.

This would mean that the statement @using MVCUI.Extensibility; in my .cshtml file and <add namespace="MVCUI.Extensibility" /> in the Web.config were referencing an old outdated dll in the bin folder that didn't have the HtmlHelper defined. This bothers still. How would I have them reference the dll in my desired output path?

Anyway, I just thought I should post about the experience and the lessons just in case other people find themselves in similar trouble. Thanks people.

Sign up to request clarification or add additional context in comments.

Comments

0

This would happen if the file with the extension method says using System.Web.WebPages (which has its own separate HtmlHelper class) rather than using System.Web.Mvc.

7 Comments

Couldn't this also be happening when the namespace is in the wrong web.config because it should be in the views' one?
Thanks SLaks. The only usings I have on the file are: using System;,using System.Collections.Generic;,using System.Web.Mvc;,using System.Web.Mvc.Html;, using System.Web.Routing;
@Silvermind: SLaks comments set me thinking along the same direction. Am trying it out. Now
Well, not the case. I don't have System.Web.WebPages in the namespaces section
@JohnGathogo You're talking about the System.Web.WebPages namespace, but you should have <add namespace="MVCUI.Extensibility"/> in the Web.Config under the Views. In the <system.web.webPages.razor><pages><namespaces> Is it there? BTW Intellisense won't recognize it before you have reloaded your project.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.