11

I have a page with several component presentations using the component template 'Promotion'. In my page view '_Page.cshtml' I wish to write out all component presentations using this template and for every second occurance write some HTML just after i've written the component presentation.

I would use the following code to write out all presentations of a certain template:

@Html.RenderComponentPresentationsByView("Promotion")

I figured I could replace this code with the following (where my Model = IPage):

@foreach (ComponentPresentation cp in Model.ComponentPresentations)
{
    if (cp.ComponentTemplate.Title == "Promotion")
    {                                   
        //Logic here to render my specific CP through HTML.RenderComponentPresentations() or something else?
    }
}

I need to replace my comment with code to achieve the same result as RenderComponentPresentationsByView but for a single component presentation.

any Ideas on how to achieve this?

Thanks in advance

10

Calling

Html.Partial("Promotion", cp);

from within your loop should do the job.

| improve this answer | |
  • Thanks very much. Your answer was correct and did provide a solution. However, I ended up with: Html.RenderPartial("../ComponentTemplateViews/MyPromotion", cp);. Notice I've given the example here with a template name 'My Promotion', and that I had to hardcode removing of the spaces. You helped me realise that calling an ActionResult inside DD4T.Mvc.Controllers.TridionControllerBase was what I needed. This way I had access to GetView() and all my calls were then consistent. See my answer for the details. Thanks again – Jonathan Primmer Jun 20 '13 at 18:27
5

I achieved this by calling the method using RenderAction from my view:

Html.RenderAction("ComponentPresentationByObject", "MyComponent", cp);

Note: This was only possible once I'd added the following method to the abstract class DD4T.Mvc.Controllers.TridionControllerBase.

public virtual ActionResult ComponentPresentationByObject(ComponentPresentation cp)
{
    try
    {
        return GetView(cp);
    }
    catch (ConfigurationException e)
    {
        return View("Configuration exception: " + e.Message);
    }
}

I inherit the abstract class TridionControllerBase in a class local to my project, MyComponent.

Strangely enough it there is a commented method in the default DD4T implementation which would seem to (nearly) provide this. I initially tried uncommenting the provided DD4T method 'TridionControllerBase.ComponentPresentation(string componentPresentationId)' but this gave me two errors (1) You must use a unique name for the action result as overloading is not permitted on Controller classes. (2) An error stating that it could not implement an interface.

Therefore I made a unique method name then changed the parameter from IComponentPresentation to ComponentPresentation.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.