Getting item index in BindTo method

4 posts, 1 answers
  1. Answer
    Ian
    Ian avatar
    11 posts
    Member since:
    Aug 2013

    Posted 08 Oct 2013 Link to this post

    I wanted to set the menu item selection based on a persisted item index but cannot see how I can access the menu item index from within the BindTo() section. I have worked around this in the code below by matching on the label but this is not ideal. Is it possible to compare the item index instead?  Thanks.

    @(Html.Kendo().Menu()
          .Name("viewTypeMenu")
          .HighlightPath(false)
          .BindTo(Model.ViewTypeLabels, (item, value) =>
          {
              item.Text = value;
              item.Selected = (value == Model.SelectedViewTypeLabel);
          })
    )
  2. nikobellic
    nikobellic avatar
    46 posts
    Member since:
    Feb 2013

    Posted 09 Oct 2013 Link to this post

    It depends on the type of "ViewTypeLabels".
    If its simply a List<string> you can use IndexOf.

    Example:
    @(Html.Kendo().Menu()
          .Name("viewTypeMenu")
          .BindTo(Model.ViewTypeLabels, (kendoMenuItem, labelValue ) => {
              kendoMenuItem.Text = labelValue;
              kendoMenuItem.Selected = (Model.ViewTypeLabels.IndexOf(labelValue) == Model.PersistedIndex);
          })
    )

  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 10 Oct 2013 Link to this post

    Hi,

    You could also use a separated parameter to track the index:

    @{
        var index = 0;
    }
     
    @(Html.Kendo().Menu()
          .Name("viewTypeMenu")
          .HighlightPath(false)
          .BindTo(Model.ViewTypeLabels, (item, value) =>
          {
              item.Text = value;
              item.Selected = index++ == selectedIndex;
          })
    )

     
    Regards,
    Daniel
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  5. Ian
    Ian avatar
    11 posts
    Member since:
    Aug 2013

    Posted 10 Oct 2013 Link to this post

    Thanks for your solutions Niko and Daniel. Cheers.
Back to Top
UI for ASP.NET MVC is VS 2017 Ready