2011-10-18 20 views
2

私は次のViewModelを持っている:私はこのスレッド次してきたRazor Viewに再帰関数を書き込む方法は?

public class AllQuestionsInCategoriesViewModel 
{ 
    public string Category_Name { get; set; } 
    public string Category_Number { get; set; } 
    public List<ShowQuestionViewModel> questions { get; set; } 
    public List<AllQuestionsInCategoriesViewModel> SubCategories { get; set; } 

    public AllQuestionsInCategoriesViewModel() 
    { 
     questions = new List<ShowQuestionViewModel>(); 
     SubCategories = new List<AllQuestionsInCategoriesViewModel>(); 
    } 
} 

ASP.NET MVC 3 Razor recursive function

と私は、このコードになってしまった:問題がある

@model List<MvcApplication3.Models.ViewModels.Category.AllQuestionsInCategoriesViewModel> 

@{ 
    Layout = null; 
} 

<!DOCTYPE html> 

<html> 
<head> 
    <title>PrintSchema</title> 
    <link type="text/css" href="../../Content/Print.css" rel="Stylesheet" /> 
</head> 
<body> 

@{ 
    foreach(var cq in Model) { 
     ShowSubItems(cq); 
    } 
} 

@helper ShowSubItems(MvcApplication3.Models.ViewModels.Category.AllQuestionsInCategoriesViewModel MyObj) 
{ 

    <h1>@MyObj.Category_Number @MyObj.Category_Name</h1> 
    foreach (var question in MyObj.questions) 
    { 
     @Html.DisplayFor(x => question, question.GetType().Name + "Print") 
    } 

    if (MyObj.SubCategories.Count != null || MyObj.SubCategories.Count != 0) 
    { 
     foreach(var subitem in MyObj.SubCategories) 
     { 
      ShowSubItems(subitem); 
     }   
    } 
} 

</body> 
</html> 

ShowSubItemsメソッドが何も表示しないことを示します。モデルは空ではなく、ViewはShowSubItemsメソッドの外で @Html.DisplayFor(x => x.question, question.GetType().Name + "Print") と表示できます。しかし、ShowSubItemsメソッドのViewへのレンダリングはありません。どうして?私はShowSubItemsへのお電話はレンダリングブロック内のコードブロックではなく内部にあるので、それはだと思う

@ShowSubItems(subitem); 
+0

はこれを試してみてください。私は出力をレンダリングするために@を使うのを忘れていました。 – Kenci

答えて

3

:ヘルパー内部も

@foreach(var cq in Model) { 
    @ShowSubItems(cq); 
} 

1

はこのようにそれを呼び出して試してみてください。この質問を忘れてください

@{ 
    foreach(var cq in Model) { 
     @ShowSubItems(cq) 
    } 
} 
関連する問題