Search code examples
asp.netasp.net-mvcasp.net-mvc-routing

Html.Action within _SharedLayout to partialView with controller


I need a _LayoutPage with a menù basd on user permission that i read from server.

i find this article and try to implement it

basically Layout call an action to controller that search user permission and return a partial view that render only some of it's element

this is the structure: structure

this the _Layout body:

<body>  
    @Html.Action("RenderMenu", "MasterController")
    <div id="bodyMasterPage">
        @RenderBody();

    </div>
    <div id="footerMasterPage">

    </div>
</body>

intestazione is my partial view, quite empty for now

MasterController:

public class MasterController : Controller
    {
        public ActionResult RenderMenu()
        {           
            {
                return PartialView("Instestazione", null);
            }
        }
    }

and TestController

public class TestController : Controller
    {
        public IUnitOfWork myUow;
        // GET: WebMVC/Test

        public TestController(IUnitOfWork uow)
        {
            myUow = uow;            
        }

        public ActionResult Index()
        {
            myUow.Area.Read(1);
            return View();
        }
    }

now when i open http://localhost:61599/WebMVC/Test/Index get this error:

 The controller for path '/WebMVC/Test/Index' was not found or does not implement IController.
Descrizione: Eccezione non gestita durante l'esecuzione della richiesta Web corrente. Per ulteriori informazioni sull'errore e sul suo punto di origine nel codice, vedere la traccia dello stack.

Dettagli eccezione: System.Web.HttpException: The controller for path '/WebMVC/Test/Index' was not found or does not implement IController.

Errore nel codice sorgente:


Riga 16: </head>
Riga 17: <body> 
Riga 18:    @Html.Action("RenderMenu", "MasterController")
Riga 19:    <div id="bodyMasterPage">
Riga 20:        @RenderBody();

which seem strange since talks of TestController and if i remove Html.Action code (@Html.Action("RenderMenu", "MasterController")) alla works fine

Why calling MasterController get error on TestController


Solution

  • problem is here: @Html.Action("RenderMenu", "MasterControler")

    Controller suffix have not to be used

    I take code from link without much attention