2011-10-18 4 views
2

나는 다음 뷰 모델이 있습니다면도기 뷰에 재귀 함수를 작성하는 방법은 무엇입니까?

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 메서드는 아무 것도 표시하지 않습니다. 모델이 비어 있지 않으며보기가 ShowSubItems 메서드 외부에서 @Html.DisplayFor(x => x.question, question.GetType().Name + "Print") 으로 잘 표시 될 수 있습니다. 그러나 ShowSubItems 메서드의 뷰에 렌더러를 가져 오는 것은 없습니다. 어떻게 됐니?

+0

이 질문을 잊지하시기 바랍니다. 나는 출력을 렌더링하는 것을 잊었다. – Kenci

답변

3

내가 ShowSubItems에 전화 코드 블록 내부가 아닌 블록을 렌더링 있기 때문에 생각 : 도우미 내부 또한

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

.

이 시도 :

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

이런 식으로 전화하십시오 :

@ShowSubItems(subitem); 
관련 문제