Telerik Treeview DataBinding

时间:2011-08-03 17:02:56

标签: c# asp.net-mvc telerik

我遇到一个孩子的数据绑定多个表的问题。例如,我说有5个表。

contracts
members
phoneassessments
homeassessments
personalassessments

每个评估表都有引用的MemberID,我需要直接在Members下的层中打印所有三种类型的评估,换句话说就是这样。

Contract 1
    Member 1
        Home Assessment 1
        Home Assessment 2
        Phone Assessment 1
        Personal Assessment 1
        etc
    Member 2

我的代码是......

Html.Telerik().TreeView()
   .Name("TreeName")
   .BindTo(Model, mappings =>
     {
       mappings.For<SMTXEFMVCModel.Contract>(binding => binding
         .ItemDataBound((item, contract) =>
           {
             item.Text = contract.Description;
             item.SpriteCssClasses = ("icon-contracts");
           })
           .Children(Contract => Contract.Members));
       mappings.For<SMTXEFMVCModel.Member>(binding => binding
         .ItemDataBound((item, member) =>
           {
             item.Text = member.FirstName + " " + member.LastName;
             item.SpriteCssClasses = ("icon-members");
        }));
     })
    )

所以我的问题是,接下来会发生什么?语法是什么?

我忍不住认为它看起来像这样。

Html.Telerik().TreeView()
   .Name("TreeName")
   .BindTo(Model, mappings =>
     {
       mappings.For<SMTXEFMVCModel.Contract>(binding => binding
         .ItemDataBound((item, contract) =>
           {
             item.Text = contract.Description;
             item.SpriteCssClasses = ("icon-contracts");
           })
           .Children(Contract => Contract.Members));
       mappings.For<SMTXEFMVCModel.Member>(binding => binding
         .ItemDataBound((item, member) =>
           {
             item.Text = member.FirstName + " " + member.LastName;
             item.SpriteCssClasses = ("icon-members");
           {)
           .Children(Members => Members.Assessments));
        mappings.For<(SMTXEFMVCModel.HomeAssessments,SMTXEFMVCModel.PhoneAssessments,
                      SMTXEFMVCModel.PersonalAssessments)>(binding => binding
         .ItemDataBound((item, assessments) =>
           {
             item.Text = HomeAssessments.AssessmentID.ToString()
             item.Text = PhoneAssessments.AssessmentID.ToString()
             item.Text = PersonalAssessments.AssessmentID.ToString()
        }));
     })
    )

这是我的控制器的相关ActionResult

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

我在我的视图中使用IEnumerable来强烈键入模型中的表。 (下面一行是我认为的第一行。

@model IEnumerable<SMTXEFMVCModel.Contract>

2 个答案:

答案 0 :(得分:1)

如果你的课程彼此之间有正确的关系,那应该很简单:

Html.Telerik().TreeView() 
            .Name("TreeName") 
            .BindTo(Model, mappings =>  
            { 
                mappings.For<SMTXEFMVCModel.Contract>(binding => binding 
                        .ItemDataBound((item, contract) => 
                        { 
                            item.Text = contract.Description;
                            item.SpriteCssClasses = ("icon-contracts");
                        }) 
                        .Children(contract => contract.Members)); 
                mappings.For<SMTXEFMVCModel.Member>(binding => binding 
                        .ItemDataBound((item, member) => 
                        { 
                            item.Text = member.FirstName + " " + member.LastName;
                            item.SpriteCssClasses = ("icon-members");
                        }) 
                        .Children(member => member.Assessments)); 
                mappings.For<MSMTXEFMVCModel.Assessments>(binding => binding 
                        .ItemDataBound((item, assessments) => 
                        { 
                            item.Text = assessments.AssessmentType;
                            ...
                        }));
            }) 

我说它应该是因为Telerik控件的确切功能在推动正常边界时会变得“模糊”,尤其是像MVC控件这样的新界限。

我确信您已经查看了示例here,但文档中没有任何内容显示了如何执行您要执行的操作的确切示例。我已经使用上面的代码工作了,但是我的模型和类可能没有像你的那样设置。

答案 1 :(得分:1)

警告,这个“解决方案”没有使用telerik树视图的数据绑定版本,但是对于任何想要在树视图中将多个表嵌套到单个层中的人来说,它似乎是一个可能的候选者。

@(

Html.Telerik().TreeView()
    .Name("SeniorMetrix")
    .Items(member => {
      foreach (var member1 in members)
            member.Add()
            .Text(member1.FirstName.ToString())
            .SpriteCssClasses("icon-members")
            .Items(occurance =>
            {
            //lets print In-Home Assessments first
            foreach (var inhomeassessment1 in inHomeAssessments)
              if (inhomeassessment1.MemberID == member1.MemberID)
              {
                occurance.Add()
                .Text("In-Home Assessment #" + inhomeassessment1.InHomeAssessmentID.ToString())
                .SpriteCssClasses("icon-assessments");
              }
            //now we print Telephonic Assessments
            foreach (var telephonicassessment1 in telephonicAssessments)
              if (telephonicassessment1.MemberID == member1.MemberID)
              {
                occurance.Add()
                .Text("Telephonic Assessment #" + telephonicassessment1.TelephonicAssessmentID.ToString())
                .SpriteCssClasses("icon-phone");
              }
            //finally, let's print Episodes
            foreach (var episode1 in episodes)
              if (episode1.MemberID == member1.MemberID)
              {
                occurance.Add()
                .Text("Episode #" + episode1.EpisodeID.ToString())
                .SpriteCssClasses("icon-episodes")
                .Items(assessment =>
                  {
                    foreach (var assessment1 in Episodes)
                      if (assessment1.EpisodeID == episode1.EpisodeID)
                      {
                        if (assessment1.AssessmentTypeID == 1)
                        {
                          assessment.Add()
                        .Text("Admission Assessment #" + afeassessment1.AssessmentID.ToString())
                        .SpriteCssClasses("icon-assessment");
                        }
                        if (assessment1.AssessmentTypeID == 2)
                        {
                          assessment.Add()
                        .Text("Interum Assessment #" + assessment1.AssessmentID.ToString())
                        .SpriteCssClasses("icon-assessment");
                        }
                        if (assessment1.AssessmentTypeID == 3)
                        {
                          assessment.Add()
                        .Text("Discharge Assessment #" + assessment1.AssessmentID.ToString())
                        .SpriteCssClasses("icon-assessment");
                        }
                        if (assessment1.AssessmentTypeID == 4)
                        {
                          assessment.Add()
                        .Text("Followup Assessment #" + assessment1.AssessmentID.ToString())
                        .SpriteCssClasses("icon-assessment");
                        }
                      }
                });
          }});
     }))