Model Class
public class DynamicMenu
{
public int MenuID { get; set; }
public string MenuName { get; set; }
public int ParentMenuID { get; set; }
}
Controller Code
public ActionResult Menu()
{
ViewData["DynamicMenu"] = new List<DynamicMenu> {
new DynamicMenu{ MenuID = 1, MenuName = "Self", ParentMenuID = 0 },
new DynamicMenu{ MenuID = 2, MenuName = "Functional", ParentMenuID = 0 },
new DynamicMenu{ MenuID = 3, MenuName = "Leave", ParentMenuID = 1 },
new DynamicMenu{ MenuID = 4, MenuName = "Leave Application", ParentMenuID = 3 },
new DynamicMenu{ MenuID = 5, MenuName = "Delete Application", ParentMenuID = 4 }
};
return View();
}
{
ViewData["DynamicMenu"] = new List<DynamicMenu> {
new DynamicMenu{ MenuID = 1, MenuName = "Self", ParentMenuID = 0 },
new DynamicMenu{ MenuID = 2, MenuName = "Functional", ParentMenuID = 0 },
new DynamicMenu{ MenuID = 3, MenuName = "Leave", ParentMenuID = 1 },
new DynamicMenu{ MenuID = 4, MenuName = "Leave Application", ParentMenuID = 3 },
new DynamicMenu{ MenuID = 5, MenuName = "Delete Application", ParentMenuID = 4 }
};
return View();
}
View Code
@{ViewBag.Title = "Dynamic Menu";
List<DynamicMenu> dynamicMenu = (List<DynamicMenu>)ViewData["DynamicMenu"];
}
<h4>
Dynamic - Menu</h4>
<div>
@using Kendo.Mvc.UI.Fluent
@functions{
public void addChildren(MenuItemBuilder builder, DynamicMenu item, IEnumerable<DynamicMenu> items)
{
var children = items.Where(m => m.ParentMenuID == item.MenuID);
if (children != null)
{
builder.Items(menuItems =>
{
foreach (var child in children)
{
var menuItem = menuItems.Add().Text(child.MenuName);
addChildren(menuItem, child, items);
}
});
}
}
}
@(Html.Kendo().Menu()
.Name("menu3")
.Items(menu =>
{
foreach (var item in dynamicMenu.Where(m => m.ParentMenuID == 0))
{
var builder = menu.Add().Text(item.MenuName);
addChildren(builder, item, dynamicMenu);
}
}))
</div>
How can this be added to the layout page?
ReplyDelete