2013-12-16 2 views
0

"Course"라는 부분 이름을 추가 한 컨텐츠 유형 "Club"을 만들었습니다. 내 컨트롤러 코드에 Club (콘텐츠 유형) 목록을 가져오고 싶습니다.과수원 CMS의 컨텐츠 유형으로 필터링

당신은 당신의 컨트롤러 IContentManager에 접근 할 필요가
public ActionResult Index(PagerParameters pagerParameters, CourseSearchVM search) 
    { 
     //this is displaying only published content 
     var courseQuery = _contentManager.Query<CoursePart>().List().ToList(); 
     // Project the query into a list of customer shapes 
     var coursesProjection = from course in courseQuery 
            select Shape.course 
            (
            Id: course.Id, 
            Name: course.Name, 
            Description: course.Description 
           ); 

     // The pager is used to apply paging on the query and to create a PagerShape 
     var pager = new Pager(_siteService.GetSiteSettings(), pagerParameters.Page, pagerParameters.PageSize); 
     // Apply paging 
     var coures = coursesProjection.Skip(pager.GetStartIndex()).Take(pager.PageSize); 
     // Construct a Pager shape 
     var pagerShape = Shape.Pager(pager).TotalItemCount(courseQuery.Count()); 
     // Create the viewmodel 
     var model = new CourseIndexVM(coures, search, pagerShape); 
     return View(model); 
    } 

답변

0

, 당신은 그냥 생성자에 추가 할 수있다 (의존성 주입을 참조 autofac은 트릭을 할 것입니다). 당신이 IOrchardServices을 사용할 수 있습니다이 외에도, 당신은 몇 가지 일반적인 서비스에 대한 액세스를 얻을 것이다 행동

 public MyController(IOrchardServices services){ 
     this.services = services; 
    } 

을 (어떻게 당신이 그것에서 더 많은 의존성의 두 가지를 사용하려는 경우 있음) 당신은 이런 식으로 뭔가를 할 수 :

 services.ContentManager.HqlQuery() 
      .ForType("Club").List() 
      .Select(ci => services.ContentManager.BuildDisplay(ci, "Summary")); 

은 첫 번째 부분은 그냥 그 후, 당신은을 보여주기 위해 다른 그 모양을 첨부 할 수 있습니다, 모양의 목록에 결과를 프로젝트, 자신의 콘텐츠 부분으로의 ContentType의 목록을 생성합니다 명부.

작업 완료 :

 var clubs = services.ContentManager.HqlQuery() 
      .ForType("Club").List() 
      .Select(ci => services.ContentManager.BuildDisplay(ci, "Summary")); 

     var shape = services.New.ClubList(); 
     shape.Clubs = clubs; 
     return new ShapeResult(this, shape); 

이것은 당신이 당신의 드라이버에 정의 된 형태의 목록입니다 속성 클럽으로 Shape를 반환합니다. ClubList에 대한보기를 만들어야합니다. 귀하의 모양 ClubList에서 다음을 수행하여 클럽 모양을 표시 할 수 있습니다.

@foreach (var club in Model.Clubs) { 
        @Display(club) 
      } 
+0

나는 위의 해결책을 시도했지만 내용 유형에 대한 기록을 가져 오지 않습니다. 작동중인 원래 게시물을 수정했지만 문제는 콘텐츠 형식 대신 콘텐츠 부분을 지정해야한다는 것입니다. 내 콘텐츠 유형이 "코스"이고 콘텐츠 콘텐츠 부분이 "코스" –

+0

ForType ("코스")을 지정해야합니다. BTW에서는 course.ContentItem을 입력하기 만하면 ContentTypes에 액세스 할 수 있습니다. 그러나 각 파트의 전체 모양을 만들려면 ContentItem을 허용하는 BuildDisplay 메서드를 호출해야합니다. – jmgomez

관련 문제