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

Set LinkHtmlAttributes from controller?

0 Answers 82 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Phil
Top achievements
Rank 1
Phil asked on 22 Feb 2012, 02:08 PM
Hi

I found an old post (http://www.telerik.com/community/forums/aspnet-mvc/treeview/treeview-databound-template.aspx) really useful and have adopted a similar approach; however, I'm also using AJAX to load child items on demand.

I cannot see how to set the LinkHtmlAttributes in my controller (for my AJAX request). LinkHtmlAttributes  is readonly and set as [ScriptIgnore]; therefore when serialized to JSON, the data is lost. The data returned from my Controller Action -  IEnumerable<TreeViewItem> - specify a URL but when bound to the treeview, the link performs a full postback.

Is there another way to set the LinkHtmlAttributes from a controller when returning data using JSON?


View
@(Html.Telerik().TreeView()
        .Name("TreeView")
        .BindTo<Release>(Model, (item, release) =>
        {
            item.Text = release.Name;
            item.Value = release.ReleaseId.ToString();
            item.LoadOnDemand = release.ChildReleases.Count > 0;
            item.Action("Dashboard", "Release", new { id = release.ReleaseId });
            item.LinkHtmlAttributes.Add("data-ajax", "true");
            item.LinkHtmlAttributes.Add("data-ajax-mode", "replace");
            item.LinkHtmlAttributes.Add("data-ajax-update", "#mainPanel");
        })
 
        .DataBinding(dataBinding => dataBinding
            .Ajax()
                .Select("_GetChildReleases", "Release"))
             
        )

Controller
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult _GetChildReleases(TreeViewItem node)
{
       IEnumerable<TreeViewItem> nodes =
           from r in db.releases
           select new TreeViewItem
           {
               Text = r.Name,
               Value = r.ReleaseId,
               LoadOnDemand = (r.ChildReleases.Count > 0),
               Enabled = true,
               Url = Url.Action("Dashboard"new { id = r.ReleaseId })
           };
             
       return new JsonResult() { Data = nodes };
 
}

Many thanks,

Phil
 
Tags
TreeView
Asked by
Phil
Top achievements
Rank 1
Share this question
or