Binding action to menu

7 posts, 1 answers
  1. ShareDocs
    ShareDocs avatar
    76 posts
    Member since:
    Oct 2012

    Posted 03 Jun 2013 Link to this post

    Hi,
    I'm trying to understand how to bind an action from the Db to the kendo menu. 
    in all the demos I saw there is and Item.add().text().action("action","controller");

    but if I bind to Db then I have:
    Item.Text = "Some Text";

    how should I attach the action method to the menu? (what should be the right side of the item.Action = ?)
    I would also like your opinion as to the way to store it in the Db (if there is any best practice regrding this matter).

    thank you
  2. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 05 Jun 2013 Link to this post

    Hi,

    You can either use the ActionName and ControllerName properties:

    item.ActionName = "Action";
    item.ControllerName = "Controller";
    or the Action method:
    item.Action("Action", "Controller", null);
    Regards,
    Daniel
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. ShareDocs
    ShareDocs avatar
    76 posts
    Member since:
    Oct 2012

    Posted 06 Jun 2013 Link to this post

    Hi,
    I tried it but the menu doesnt responds and i don't get anything on fiddler too...

    is there anything to pay attention when using actions? (I don't get an error, just a menu that does not redirect me :( )

    UPDATE:
    I managed to get it to work.
    I've set the parent mapping action and controller and it's now working (why I can't set it through the childs if the childs is what I want to get the action?)


  5. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 10 Jun 2013 Link to this post

    Hello Lior,

    I am not sure what could be causing the described behavior. Could you provide the code you are using?

    Regards,
    Daniel
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  6. ShareDocs
    ShareDocs avatar
    76 posts
    Member since:
    Oct 2012

    Posted 10 Jun 2013 Link to this post

    what part of the code do you want?

    this is the view
    @model IEnumerable<EladFI.Domain.Entities.Key_MenuItems>
     
     
    @(Html.Kendo().Menu()
          .Name("Menu") //The name of the menu is mandatory. It specifies the "id" attribute of the widget.
          .BindTo(Model , mappings =>
          {
              mappings.For<EladFI.Domain.Entities.Key_MenuItems>(binding => binding //define first level of menu
                  .ItemDataBound((item, category) => //define mapping between menu item properties and the model properties
                      {
                      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))); //define which property of the model contains the children
               
              mappings.For<EladFI.Domain.Entities.Key_MenuItems>(binding => binding
                  .ItemDataBound((item, product) =>
                  {
                      item.Text = product.Menu_Title;
                  }));
          })
          
               
    )

    this is the Key_MenuItems entity:

    public class Key_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; }
     
            public virtual Key_MenuItems Parent { get; set; }
     
            public virtual ICollection<Key_MenuItems> ChildMenuItems { get; set; }
        }

    this is the layout which contains the calling to the controller method that brings the partialView:

    <div class="float-right">
                @{Html.RenderAction("Menu", "Menu");}
               </div>

    Hope you'll find the reason... 
  7. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 12 Jun 2013 Link to this post

    Hello Lior,

    The same type is specified for both mappings. The Menu will automatically bind the children on all levels for homogeneous data and so the second mapping will not be taken into account. If the action should not be used for the top level items then you should the approach that it seems you are already using and set the action and controller configuration only when needed.

    Regards,
    Daniel
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  8. Answer
    ShareDocs
    ShareDocs avatar
    76 posts
    Member since:
    Oct 2012

    Posted 12 Jun 2013 Link to this post

    I understand now,
    thank you
Back to Top
UI for ASP.NET MVC is VS 2017 Ready