2
votes

I just wondering how can I get an specific ContentItem in my controller.

I want to get the specific content Item and then display it's shape on my custom View.. PS: I also dont know how to get the ID of the content item should i use the ContentManager.Get(ID)

    [HttpGet]
    public ActionResult Index(string jobType, string location) {

        var vm = new SearchForJobViewModel();

        var items = new List<CustomPart>();

        // Load the WhatsAround content items
        IEnumerable<ContentItem> whatsAroundContentItems = ContentManager.Query().ForType("Custom").List();

        foreach (ContentItem contentItem in whatsAroundContentItems)
        {
            ContentItemRecord contentItemRecord = contentItem.Record;
            if (contentItem == null)
                continue;

            //CustomPart item = new CustomPart(contentItemRecord.Data);
            //items.Add(item);
        }

        //Im also planning to pass the ContentItem in the view and then render it there
        //return View("../JobSearchResults", items.Single(i => i.Name == "MyContentItem");
        return View("../JobSearchResults", vm);
    }

ADDITIONAL

    [Themed]
    [HttpGet]
    public ActionResult Index(string jobType, string location) {

        var vm = new SearchForJobViewModel();

        vm.SelectedJobType = jobType;
        vm.SelectedLocation = location;

        //query all the content items
        IEnumerable<ContentItem> items = ContentManager.Query().List();

        foreach (ContentItem contentItem in items) {

            ContentItemRecord contentItemRecord = contentItem.Record;

            if (contentItem == null)
                continue;

            if (contentItemRecord.ContentType.Name == "CustomPage") {

                // I just painfully search the contents just to get the ID of the specific content item that I want to display
                // I dont know what table where I can see the ID on the content items
                if (contentItemRecord.Id == 40) {
                    ContentItem ci = contentItem;

                    var test = ci.As<CustomPart>().Scripts; //custom part that I made

                    // the body part (raw html from the wysiwg editor)
                    // this I wil render it in my view
                    vm.Body = ci.As<BodyPart>().Text;


                }
            }
        }

        return View("../JobSearchResults", vm);
    }

JobSearchResults.cshtml

     @Html.Raw(Model.Body)

It's actually looks wierd but I'm trying something like utilizing Orchard CMS without using Orchard core (contentpart, drivers,handlers, etc)

1
It's really not clear what you are trying to do, what you tried and how it failed.Bertrand Le Roy
well, I'm just trying to get an specific Content Item and display it in my View. Please see my edit..lincx
in what specific table I can get the content item's ID where I can be able to use in contextManager.Get(ID)lincx
Problem is, your code is extremely inefficient: you are getting all content items, and then you are looping over them to find the one you want. Instead, learn how to use the Query API so that you don't have to do that horrible loop. Finding the ID is the least of your problems, looking at this code, but each part has an ID property that is also the ID of the content item. Also, you don't even need to get the content item by ID, as if you have a reference to a part, you can already get to the content item, and to any of its parts.Bertrand Le Roy

1 Answers

1
votes

Your can refer this. http://skywalkersoftwaredevelopment.net/blog/getting-the-current-content-item-in-orchard

Or using the driver

protected override DriverResult Display(YourModulePart part, string displayType, dynamic shapeHelper){
    var title = part.As<TitlePart>();
    //here you can access the title part.
}