开发者

Nested Children in Telerik Treeview with MVC 3.0

I have a fairly simple data model with three tables.. Contracts, Members, Episodes. And am trying to build a telerik treeview to show every entry in the three tables with respect to their relationship in tiers of the tree...

  • There are multiple Contracts.
  • Each Contract has multiple Members.
  • Each Member has Multiple Episodes.

As a note, the telerik demo only shows one section of children. Telerik's Online Demo

Additionally, their Drag and Drop demo uses the same tables and simply uses a GetRootEmployee function to pupulate. So I can't find any relevant examples.

Example:

Contract 1's decription
     Member of Contract 1's name    
          Episodeid of Member in Contract 1
     A开发者_如何学编程nother Member of Contract 1...
          Episodeid of another Member.
Contract 2's description

The problem is that I simply cannot get the episodes (third tier) to populate successfully, though the first two work fine. I will post my View, and my controller.

View

 @(
    Html.Telerik().TreeView()
   .Name("myTree")
   .BindTo(Model, mappings =>
     {
       mappings.For<SMTXEFMVCModel.Contract>(binding => binding
         .ItemDataBound((item, contract) =>
           {
             item.Text = contract.Description;
           })
           .Children(contract => contract.Members));
       mappings.For<SMTXEFMVCModel.Member>(binding => binding
         .ItemDataBound((item, member) =>
           {
             item.Text = member.FirstName + " " + member.LastName;
           })  //If I stop here, it populates Contract and Members correctly.
           .Children(member => member.Episodes));
       mappings.For<SMTXEFMVCModel.Episode>(binding => binding
         .ItemDataBound((item, episode) =>
           {
             item.Text = episode.episodeID;
           }));
     })
)

Controller

      public ActionResult TreeView()
  {
      var ctx = new SMTXContext();
      var Contracts = ctx.Contracts.ToList();

      return View(Contracts);
  }


Looks like the problem was a datatype mixup.

item.Text = episode.episodeID;

should have been

item.Text = episode.episodeID.ToString();

then it works flawlessly.


your third mapping seems to be buggy

 mappings.For<SMTXEFMVCModel.Member>(binding => binding
         .ItemDataBound((item, episode) =>
           {
             item.Text = episode.episodeID;
           }));

shouldn't it define mapping for Episode rather than Member. Furthermore there is no need for

var Members = ctx.Members.ToList();
      var Assessments = ctx.Assessments.ToList();

when you are not passing these values to view in any way possible like

return View(Contracts);

Treeview will automatically traverse through Members and Episodes objects through navigational properties of Contract and Member object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜