Click here to Skip to main content
15,032,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to bind my menu's retrieved from my LINQ query to menus in Layout.

In Asp.net whenever i had such requirement i use to write dynamic menu binding code in load event of masterpage.

In mvc i understand that the request first goes to controller and then Appropriate view is loaded but In my case there are two pages which i need to load
1. Layout page(Master Page) with Appropriate Menus
2. Child page.(Which has any basic features.)
Here i am not able to understand which code to write where.


Example
1. where to write code for loading menu in Layout page?
2. How to call code for loading menu?
3. When to call code for Loading menu of Layout page and when & where to call my actual child page Action?

What I have tried:

public JsonResult GetMenu()
 {
     int LevelId = 1;

     GEContext gc = new GEContext();
     List<Menu> menuList = gc.Menus.Include("MenuRights").ToList();

     var pageObject = (from op in gc.Menus
                       join mr in gc.MenuRights on op.MenuId equals mr.MenuId
                       //join us in gc.Users on mr.LevelId equals us.LevelId
                       where mr.LevelId == LevelId
                       select new { op });

     return Json(pageObject, JsonRequestBehavior.AllowGet);
 }


public class Menu
 {
     [Key]
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
     public int MenuId { get; set; }


     [StringLength(20)]
     public string MenuName { get; set; }

     [StringLength(50)]
     public string ActionName { get; set; }

     [StringLength(50)]
     public string ControllerName { get; set; }

     [StringLength(200)]
     public string MenuUrl { get; set; }

     public int ParentId { get; set; }

     [StringLength(10)]
     public string TargetWindow { get; set; }
     //public MenuRights MenuRights { get; set; }

     [StringLength(20)]
     public string ProjectCode { get; set; }

     [ForeignKey("ProjectCode")]
     public Project Project { get; set; }

 }


public class MenuRights
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int MenuRightsId { get; set; }


    public int MenuId { get; set; }

    public int LevelId { get; set; }

    [ForeignKey("LevelId")]
    public Level Level { get; set; }

    [ForeignKey("MenuId")]
    public Menu Menu { get; set; }

    [StringLength(20)]
    public string ProjectCode { get; set; }

    [ForeignKey("ProjectCode")]
    public Project Project { get; set; }

}
Posted
Updated 28-Nov-17 3:49am
v3

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