0

I have a main mvc project and an area

The Area uses the shared _Layout.cshtml from the main project. In the shared _Layout.cshtml, there is a RenderPartial("GlobalNavigation","Navigation") that call the controller "Navigation" in the main project. So I got this error

The IControllerFactory 'abc.NinjectControllerFactory' did not return a controller for the name 'Navigation'.

I guess its because the view is calling the Controller "Navigation" in the Area, but the controller "Navigation" is in the main project. How can I fix this?

_Layout.cshtml

<div id="global-nav">
    @{ Html.RenderAction("GlobalNavigation", "Navigation"); }
</div>
tereško
  • 58,060
  • 25
  • 98
  • 150
kaboom
  • 833
  • 4
  • 18
  • 38

2 Answers2

2

Try this:

<div id="global-nav">
    @{ Html.RenderAction("GlobalNavigation", "Navigation", new { area = "" }); }
</div>
ataravati
  • 8,891
  • 9
  • 57
  • 89
  • Thanks. what is the "area" in that line means? – kaboom Aug 02 '13 at 18:01
  • Well, I don't understand your question. "area" is the Area in your MVC project. When you set `area = ""`, you are actually telling the asp.net server to use the controller in the "main project". – ataravati Aug 02 '13 at 19:03
  • 1
    You pass `new { area = "" }` as an object that represents your RouteValues. You can specify the controller and action in a RouteValue as well. Like this: `Html.RenderAction("GlobalNavigation", new { controller = "Navigation", area = "" });` – ataravati Aug 02 '13 at 19:06
  • 1
    You can read about routing here: http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs – ataravati Aug 02 '13 at 19:11
0

Why do you need the controller argument if you're just loading a partial? Is there logic being build in the controller method?

Try just:

@{ Html.RenderAction("GlobalNavigation"); }

Other wise, I'd suggest using an HTML helper in your project to build our your main navigation.

eg.

Helper in PROJECT.Web.ExtensionMethods.HtmlExtensions

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace OHTP.Web.ExtensionMethods
{
    public static class HtmlExtensions
    {
        public static MvcHtmlString NavMenuLink(this HtmlHelper helper, string linkText, string actionName, string controlName, string activeClassName)
        {
            if (helper.ViewContext.RouteData.Values["action"].ToString() == actionName &&
                helper.ViewContext.RouteData.Values["controller"].ToString() == controlName)
            {
                var menuLink = helper.ActionLink(linkText, actionName, new { Controller = controlName }, new {Class = activeClassName}); 
                return menuLink;
            }

            return helper.ActionLink(linkText, actionName, controlName);
        }
    }
}

Then call it in the partial with

<%= Html.NavMenuLink("Copy to display", "ControllerName", "Link", "class") %>
Christopher Marshall
  • 10,678
  • 10
  • 55
  • 94