0
votes

Is there some way in Sitecore 7 when using MVC to create a controller which can return a PartialView result using the rendering defined on the item (or its template standard values), without having to set up Layout and Renderings for that item?

I can see how to do this using custom template fields, but this seems like a hacky way of doing things. Is there a better way to achieve the following:

public PartialViewResult MyAction(string someParameter)
{
    Item selectedItem;
    //some code here to retrieve sitecore item based on the value of someParameter

    var cshtmlFilePath = selectedItem["MyCustomField"];

    return PartialView(cshtmlFilePath, selectedItem);
}

FYI my cshtml could be something really simple like:

@model Sitecore.Data.Items.Item
<div>
    <h3>
        @Html.Sitecore().Field("Title", Model)
    </h3>
    <div>
        @Html.Sitecore().Field("Content", Model)
    </div>
</div>

While the approach above will work, I don't like it because there is not a proper interface for assigning the rendering to the item's template's standard values. I could easily define my own presentation template which has a single 'Path' field, then create items somewhere under `sitecore/layout/Renderings' then reference that item in a link field of my content item, but it just seems like that should be doable out of the box.

2

2 Answers

0
votes

I'm not sure I fully understand the intent here, so this may only be a partial answer, but seeing as you want to render an item based on layout information contained within the item itself, would Item Renderings be able to help?

Here's the info: http://www.sitecore.net/Community/Technical-Blogs/John-West-Sitecore-Blog/Posts/2012/06/MVC-Item-Renderings-in-the-Sitecore-ASPNET-CMS.aspx

I realize returning a partial view would be a little more flexible than the Item Rendering route, but in the normal scenario (i.e. adding a callout to a page) it seems like this method might work.

0
votes

One solution is to simply assign a layout (which references a cshtml file) to the item, then use item.Visualization to get the file path to the layout assigned to the item and use that layout's cshtml file in the call to PartialView.

public PartialViewResult MyAction(string contentItem)
{
    Item selectedItem;
    //retrieve sitecore item
    string layoutPath = selectedItem.Visualization.Layout.FilePath;
    return PartialView(layoutPath, modalContentItem);
}