Lorenzo
Lorenzo

Reputation: 29427

MVC extension unrecognized

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?

Upvotes: 1

Views: 1354

Answers (1)

Rodrigo Juarez
Rodrigo Juarez

Reputation: 1795

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

Upvotes: 1

Related Questions