John Gathogo
John Gathogo

Reputation: 4655

Using custom html helpers in an ASP.NET MVC 3 Application

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.

Upvotes: 2

Views: 2878

Answers (2)

John Gathogo
John Gathogo

Reputation: 4655

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.

Upvotes: 1

SLaks
SLaks

Reputation: 887453

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.

Upvotes: 0

Related Questions