1

I am building a side menu page wuth the following code

<nav class="navbar-default navbar-static-side" role="navigation">
    <div class="sidebar-collapse">
        <ul class="nav" id="side-menu">
            @if ( User.IsInRole( Constants.ROLE_ADMIN ) ) {
                <li class="@Html.IsSelected(controller: "UserGroup")">
                    <a href="@Url.Action("Index", "UserGroup")"><i class="fa fa-user"></i> <span class="nav-label">Users and Roles</span> <span class="fa arrow"></span></a>
                    <ul class="nav nav-second-level">
                        <li class="@Html.IsSelected(action: "Index")"><a href="@Url.Action("Index", "UserGroup")">Users</a></li>
                        <li class="@Html.IsSelected(action: "CreateUser")"><a href="@Url.Action("CreateUser", "UserGroup")">Create user</a></li>
                    </ul>
                </li>
            }
        </ul>
    </div>
</nav>

The Html.IsSelected extension method simply add a css class to the item to set as selected by looking at the controller and/or action

namespace MyWebApp.Helpers
{
    public static class BootstrapExtensions
    {
        public static string IsSelected( this HtmlHelper html, string controller = null, string action = null ) {
            string cssClass = "active";
            string currentAction = (string)html.ViewContext.RouteData.Values["action"];
            string currentController = (string)html.ViewContext.RouteData.Values["controller"];

            if ( String.IsNullOrEmpty( controller ) )
                controller = currentController;

            if ( String.IsNullOrEmpty( action ) )
                action = currentAction;

            return controller == currentController && action == currentAction ?
                cssClass : String.Empty;
        }
    }
}

This works very good except for the fact that visual Studio still mark the extension method as unrecognized while developing. If I build the project everythings works, but then, after the build finish VS sign the code as not recognized.

enter image description here

I have added an using on top of the view as well as added the namespace in the root Views web.config file

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <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="MyWebApp" />
            <add namespace="MyWebApp.Resources" />
            <!-- extensions namespace -->
            <add namespace="MyWebApp.Helpers" />
        </namespaces>
    </pages>
</system.web.webPages.razor>

Any suggestion?

Lorenzo
  • 29,081
  • 49
  • 125
  • 222
  • This question doesn't have anything to do with bootstrap. – ataravati Apr 18 '15 at 13:46
  • Sometimes these problems resolve when you restart Visual Studio. – ataravati Apr 18 '15 at 13:48
  • @Right. I have removed the tag and corrected the title. However this is not my case. I have restarted VS a couple of tiems until now – Lorenzo Apr 18 '15 at 13:56
  • Make sure the namespaces match. Maybe post the extension class as well? – jamesSampica Apr 18 '15 at 20:39
  • @Shoe: thanks for trying to help. I have modified the question by adding the class and namespace. Obviously there are other extension methods which I have removed. I double and triple checked and the namespace is identical. – Lorenzo Apr 18 '15 at 21:39

1 Answers1

1

If the problem is the namespace, try piggybacking to another used namespace, for example System

namespace System
{
    public static class BootstrapExtensions
    {

Also try to run the project in debug mode, some of my projects have problems taking new code when run in release mode

Rodrigo Juarez
  • 1,745
  • 1
  • 23
  • 38
  • Hi, thanks for helping. I have followed your suggestion and something interesting happened. I have moved the extension class in the `System.Web.Mvc` namespace and the problem disappeared. However I have also other extension methods in that class and one of these use an utility class which is in the `MyWebApp.Helpers` namespace. I have then tried to add a simple `using MyWebApp.Helpers` on top of the extension class file and the compiler mark the `Helpers` as error! What is going to happen? Also I am working in debug. – Lorenzo Apr 18 '15 at 22:05