I have downloaded an application In where I have _Layout.cshtml and in this _Layout.cshtml I am calling a Partial View _Hearder the Partial view code is as below
Now I want to made this _Header to get data from data base table what should i do ? i am newby MVC developer
What I have Done :
I have done a demo project in which i have created a Menu controller and i have already add entity data model which was basically my menu table then my controller is having that code
Menu Controller :
private TestEntities testEntities = new TestEntities();
public ActionResult Index()
{
ViewBag.MenuLevel1 = this.testEntities.Menus.Where(menu=>menu.ParentId==null ).ToList();
return View();
}
and I have a Folder Inside Views Folder I have Menu Folder and in Menu folder i have Index view having below code
@Model Menu.ModelTest
- @menuLevel1.Name
@if (menuLevel1.Child.Count > 0)
{
-
@menuLevel2.Name
@foreach (var menuLevel2 in menuLevel1.Child)
{
}
}
-
@foreach (var menuLevel1 in ViewBag.MenuLevel1)
{
}
This runs fine i want to made this as partial view and then I want to call this partial view from _Layout , I have know how about simple Partial view but this Partial View is having Controller Action method as well so how to call This Partial view from _Layout.cshtml whatever my _Layout.cshtml is not using any Model.

Mark TaborPosted May 13, 2016, 4:55 AM
Saineshwar BageriPosted May 13, 2016, 3:52 AM
Mark TaborPosted May 13, 2016, 3:49 AM
Saineshwar BageriPosted May 13, 2016, 3:43 AM
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
Mark TaborPosted May 13, 2016, 3:38 AM
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'.
Saineshwar BageriPosted May 13, 2016, 3:24 AM
@{ Html.RenderAction("DisplayMenu", "Menu");}Mark TaborPosted May 13, 2016, 3:21 AM
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")Saineshwar BageriPosted May 13, 2016, 2:14 AM
{
[ChildActionOnly]
public ActionResult DisplayMenu()
{
return PartialView("PartialViewName", Model);
}
}