在Orchard CMS中按内容类型过滤 [英] Filter by Content Type in Orchard CMS

查看:67
本文介绍了在Orchard CMS中按内容类型过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了内容类型"Club",并向其中添加了部件名称"Course".我想在我的控制器代码中获得俱乐部(内容类型)的列表.

I have created a content type "Club" to which i have added part name "Course". i want to get list of Club (content type) in my controller code.

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

推荐答案

您必须访问控制器中的IContentManager,您只需将其添加到构造函数中即可(请参阅依赖项注入,autofac可以解决问题).除此之外,您还可以使用IOrchardServices,它将使您能够访问一些常用服务(如果要使用其中的两个以上依赖项,请执行此操作)

You have to get access to IContentManager in your controller, you can do it just adding it to the constructor (see dependency injection, autofac will do the trick). In addition to this you can use IOrchardServices, it will get you access to a few common services (do that if you want to use two of more dependencies from it)

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

在您的操作中,您可以执行以下操作:

In your action you can do something like this:

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

第一部分将创建ContentType及其内容部分的列表,然后将结果投影到形状列表中,然后,您可以将这些形状附加到另一个形状上以显示该列表.

The first part will create a list of your ContentType with their content parts, then it just projects the result to a list of shapes, after that, you can attach those shapes to another in order to show the list.

完成动作:

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

这将返回带有属性Clubs的形状,这是您在驱动程序上定义的形状的列表.请注意,您必须为ClubList创建一个视图.在形状ClubList中,您可以通过执行以下操作显示俱乐部形状:

This will return a shape with a property Clubs which is the list of shapes that you defined on your Drivers. Note that you have to create a view for ClubList. In your shape ClubList you can display your clubs shapes by doing:

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

这篇关于在Orchard CMS中按内容类型过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆