Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have a demo project in which i have rendered a menu dynamically from database using Entity data Model I have Add a Model into my Models Folder and in this model i have my menu table (id,parentId,title)
then i have made a menu controller like that
JavaScript
private TestEntities testEntities = new TestEntities();
public ActionResult Index()
{
ViewBag.MenuLevel1 = this.testEntities.Menus.Where(menu=>menu.ParentId==null ).ToList();
return View();
}


and in my View--->Menu--->index i have that code
HTML
@Model Menu.ModelTest
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<ul>
@foreach (var menuLevel1 in ViewBag.MenuLevel1)
{
<li>@menuLevel1.Name
@if (menuLevel1.Child.Count > 0)
{
<ul>
@foreach (var menuLevel2 in menuLevel1.Child)
{
<li>
@menuLevel2.Name
</li>
}
</ul>
}
</li>
}
</ul>
</div>
</body>
</html>

its working fine and did the good job , Now i want to create a Partial View of that because i want this menu to be called or placed any where any time how to do that .

Basically i am newby in MVC i want the layout file just like a master page in asp.net and i want the menu to be dynamic in this layout file. any help please , I know about partial views but a partial view which is dynamic i mean which shows data from database and having controller action method as well how to call that

What I have tried:

I have tried to create a partial view strongly type from menu table and then html.Partial("ViewName") it did not work
Posted
Updated 12-May-16 7:18am
v3

1 solution

I am not sure if you really have a partial view or you are using the existing "View" to be rendered as partial view.
The Razor syntax to render a partial view is
C#
@Html.Partial("PartialView")  

or
C#
@{Html.RenderPartial("PartialView");} 

I think you are creating a "View" and rendering it as you render a "Partial View". A partial view is a special reusable view and it is placed inside "Shared" folder. When you create a view you will have to check "Create as Partial View" checkbox.

Check this link[^] for a complete process of creating and rendering partial view.

Hope it helps.
 
Share this answer
 
Comments
Malikdanish 12-May-16 9:43am    
Dear As i stated i have download a project in which in the _Layout view it is calling a partial view for menu item but it is static view i want the menu to be dynamic comes from database table using the entity data model , How to call a partial view having a controller action as well ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900