2011-08-03 65 views
1

我有問題的數據綁定多個表中的一個兒童teir。所以說例如我有5張桌子。Telerik Treeview DataBinding

contracts 
members 
phoneassessments 
homeassessments 
personalassessments 

每個評估表具有參考MEMBERID,我需要打印所有三種類型的分層評估的直屬會員,換句話說就是這個樣子。

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() 
     })); 
    }) 
    ) 

下面是我的控制器

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

,我在我看來使用作爲一個IEnumerable強類型從模型中的表相關的ActionResult。 (下面的線是在我看來,第一行

@model IEnumerable<SMTXEFMVCModel.Contract> 

回答

1

警告,這個「解決方案」沒有使用telerik treeview的數據綁定版本,但是對於任何想在樹視圖中將多個表格嵌套到單層的人來說,這似乎是一個可能的候選人。

@(

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"); 
         } 
         } 
       }); 
      }}); 
    })) 
+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控件這樣的新界面。顯示確切的文檔如何做你想做的事情的例子。我已經使用上面的代碼得到了這個工作,但是我的模型和類可能不會像您一樣設置。

+0

這將列出各成員所有的評估(如果我存儲所有我評估一個表這將是偉大的,但我期待在拉動價值評估的3個不同的表(手機,電話,個人)到這一層,我需要所有三個映射..我會編輯我的帖子,以顯示我覺得它可能看起來像我雖然我不能得到它的工作 – Kulingar

+0

似乎它可以提供工作提供你的模型是正確設置的Members.Assessments。你能發佈更多關於你的模型結構的信息? –

+0

你到底想知道什麼?我對這個術語有點新鮮,你想要表名,關係(從1到多),從實體框架中的模型創建的類名? – Kulingar

相關問題