Skip to content
Loading
How to call A partial view from _Layout.cshtml {partial view
0
  • Saineshwar Bageri
    then it is showing or not
    0
  • y Model is as below and my partial view code is i have now include IEnumerable which was not previouslly added .my partial menu code is
    @model IEnumerable
      @foreach (var menuLevel1 in ViewBag.MenuLevel1)
      {
    • @menuLevel1.Name
    • @if (menuLevel1.Child.Count > 0)
      {
        @foreach (var menuLevel2 in menuLevel1.Child)
        {
      • @menuLevel2.Name
      • }
        }
        }
        my
        0
      • Saineshwar Bageri
        Check model which you are passing to Partial view
        if you are using collection of model then pass collection from collection of model and
        if you are passing simple model then check you Partial view should take simple model as input
        0
      • Thanks you for your continous response Saineshwar I have changed the Controller Name from MenuController to Menu Now i am getting that error

        The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[BusinessApplicationTemplate.Models.Menu]', but this dictionary requires a model item of type 'BusinessApplicationTemplate.Models.Menu'.

        0
      • Saineshwar Bageri
        Hi Mark Tabor,
        The Controller name must be Menu not MenuController
        Just make this change.
        @{ Html.RenderAction("DisplayMenu", "Menu");}
        0
      • Thanks For Support Now What I did I simple Add an entity Data model into my project in models folder basically this entity data model bind the table from database {menu table} then i create a controller MenuController
        private PakeezaSodagranEntities3 pk = new PakeezaSodagranEntities3();
        [ChildActionOnly]
        public ActionResult DisplayMenu()
        {
        ViewBag.MenuLevel1 = this.pk.Menus.Where(menu => menu.ParentId == null).ToList();
        return PartialView("Menu", pk.Menus.ToList());
        }
        Then i add a partial view in my Shared folder with name Menu
        the code of the partial view is
        @model BusinessApplicationTemplate.Models.Menu
          @foreach (var menuLevel1 in ViewBag.MenuLevel1)
          {
        • @menuLevel1.Name
        • @if (menuLevel1.Child.Count > 0)
          {
            @foreach (var menuLevel2 in menuLevel1.Child)
            {
          • @menuLevel2.Name
          • }
            }
            }
            and then in my Layout.cshtml i am calling this partial view as
            @{ Html.RenderAction("DisplayMenu", "MenuController");}
            its gives an exception like that

            The controller for path '/' was not found or does not implement IController.

            Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

            Exception Details: System.Web.HttpException: The controller for path '/' was not found or does not implement IController.

            Source Error:

            Line 31:  Line 32:     @* @Html.Partial("_Header")*@ Line 33:    @{ Html.RenderAction("DisplayMenu", "MenuController");} Line 34:     @RenderBody() Line 35:     @Html.Partial("_Footer")
            0
          • Saineshwar Bageri
            Hi Mark Tabor,
            First create a ActionMethod in Controller that will return you and Partial view.
            public class MenuController : Controller
            {

            [ChildActionOnly]
            public ActionResult DisplayMenu()
            {
            return PartialView("PartialViewName", Model);
            }

            }
            and then call this ActionMethod on Layout as below
            @{ Html.RenderAction("DisplayMenu", "MenuController");}
            This will solve your Issue
            0