This is a migrated thread and some comments may be shown as answers.

[Solved] MVC Menu and bind to model problem

3 Answers 157 Views
Menu
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Tuinel
Top achievements
Rank 1
Tuinel asked on 16 Mar 2012, 02:43 AM
I had some nice simple menu
@(Html.Telerik().Menu()
.Name("TestMenu")
.Items(items =>
{
    items.Add().Text("Home").Action("Index", "Home");
    items.Add().Text("Metryki").Action("Index", "Metryki")
        .Items(sub => {
            sub.Add().Text("Engine").Action("Index", "MetrykiEngine");
        });
    items.Add().Text("About").Action("About", "Home");
    items.Add().Text("Contact").Action("Contact", "Home");
    items.Add().Text("Settings")
        .Items(sub =>
        {
            sub.Add().Text("Languages").Action("Index", "Language");
            sub.Add().Text("Machine Groups").Action("Index", "MachineGroup");
            sub.Add().Text("Machine Model").Action("Index", "MachineModel");
            sub.Add().Text("Machine Destination").Action("Index", "MetrykiDestination");
            sub.Add().Text("Machine Conditions").Action("Index", "MetrykiCondition");
            
        });
})
)
Now  I want put all in database and achieve thesame effect
Repository
 
        public IQueryable<MenuViewModel> AllViewModel
        {
            get
            {
                var menuVM = context.Menus
                    .Select(m => new MenuViewModel
                    {
                        MenuID = m.MenuID,
                        MenuNazwa = m.MenuNazwa,
                        MenuActionID = m.MenuActionID,
                        MenuActionName = m.MenuAction.MenuActionName,
                        MenuControlID = m.MenuControlID,
                        MenuControlName = m.MenuControl.MenuControlName,
                        MenuParentID = m.MenuParentID
                    });
 
                return (menuVM);
            }
        }
ViewModel  
 public class MenuViewModel
    {
        public int MenuID { get; set; }
        public int MenuParentID { get; set; }
        public string MenuNazwa { get; set; }
        public int MenuControlID { get; set; }
        public string MenuControlName { get; set; }
        public int MenuActionID { get; set; }
        public string MenuActionName { get; set; }
    }

 trying bind to model and im totally lost :) .. any idea how make this working ?


3 Answers, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 16 Mar 2012, 02:05 PM
Hello Tuinel,

Please check this demo project which shows how to bind the Menu to a Model.
Since you have self-referencing hierarchy you need to pass only the root level of the menu items (i.e. where MenuParentID is null) to the BindTo method and declare that the Children of the MenuViewModel 
items is a collection property of MenuViewModel.
Also check this TreeView demo which shows the same self referencing scenario.

All the best,
Petur Subev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
0
Tuinel
Top achievements
Rank 1
answered on 16 Mar 2012, 06:57 PM
Tx for answer, i will fight a little bit with this topic :), because stil problem is with [Authorize] filter. In first case menu item is not generated when user dont have rights to controller -> action, in BoudTo Model items just react like standard jQuery (etc) menu :), or maby is some solution which i dont know :)
0
ShareDocs
Top achievements
Rank 1
answered on 04 May 2013, 02:59 PM
Hi, Tuinel >> did you managed to get a working example?

can you please post it? 

i am struggling with the telerik example with no luck...

EDIT:
since I solved this I decided to publish my solution :)

The View:
@(Html.Kendo().Menu()
      .Name("Menu")
      .BindTo(Model, mappings =>
      {
          mappings.For<MenuItems>(binding => binding
              .ItemDataBound((item, category) =>
                  {
                      item.Text = category.Menu_Title;
                      item.ImageUrl = category.Menu_Image ?? "";
 
                      if (category.Action != null)
                      {
                          item.ActionName = category.Action.Split('|')[0];
                          item.ControllerName = category.Action.Split('|')[1];
                      }
                       
                  })                 
              .Children(category => category.ChildMenuItems.Where(x => x.Is_Active && x.Action != null)));
          mappings.For<MenuItems>(binding => binding
              .ItemDataBound((item, product) =>
              {
                  item.Text = product.Menu_Title;
              }));
      })
   )



The Controller:
public ActionResult Menu(UserDetails user)
      {
           
          MenuViewModel menuV = new MenuViewModel();
          return PartialView(menuV.GetMenuHirr(repository));
      }

The ViewModel:
public IEnumerable<MenuItems> GetMenuHirr(IMenuItemRepository repository)
     {          
             var menuHirr  = repository.AllMenuItems.Where(x => x.Parent_ID == null && x.Is_Active && !x.ChildMenuItems.All(c => c.Action == null)).OrderBy(s => s.Menu_Order).ToList();
             return menuHirr;
     }

The Entity:
public class MenuItems
    {
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity), Key()]
        public int ID { get; set; }
 
        public string Menu_Title { get; set; }
 
        public int? Parent_ID { get; set; }
 
        public string Action { get; set; }
 
        public int Menu_Order { get; set; }
 
        public bool Is_Active { get; set; }
 
        public string Menu_Image { get; set; }
 
        public bool QA_Only { get; set; }
 
        public bool Is_Blank { get; set; }
 
        [ForeignKey("Parent_ID")]
        public virtual MenuItems Parent { get; set; }
 
        [ForeignKey("Parent_ID")]
        public virtual ICollection<MenuItems> ChildMenuItems { get; set; }
    }


Hope this helps anyone
Tags
Menu
Asked by
Tuinel
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Tuinel
Top achievements
Rank 1
ShareDocs
Top achievements
Rank 1
Share this question
or