0

I have a page in sitecore called SingleField.cshtml. It only has a 1 field from the content item that it's associated to.

@Html.Sitecore().Field("Title")

What I am searching for is how to dynamically tie a MVC page to a content item. Is there a way to do this? One thought is to pass in a content item id to the MVC page. Is there any API call like the following?

@Html.Sitecore().SetContentItem("f358f263-ee1b-4c40-a444-540d3fb636d9") @Html.Sitecore().Field("Title")

2 Answers 2

2

Use the rendering datasource

I would recommend not tying an item directly to a rendering, in code, in the rendering. The way that Sitecore MVC works is that the context for @Html.Sitecore() is determined by the datasource being set or not.

If datasouce not set, @Html.Sitecore() = current page/item

If datasouce set, @Html.Sitecore() = datasource item

How to do it

I would recommend that you set the Title the way you currently are.

<div class="event__title">
    @Html.Sitecore().Field("Title")
</div>

Then set the datasource in the rendering itself. Not on the rendering in the page, but in the actual rendering item. This "hard codes" the datasource to the rendering. But it doesn't take a deployment to change it.

enter image description here

Override it

You can override the item by setting the datasource in the traditional way.

1

There's an overloaded version that allows you to pass in the item. So you could just put in a call first to get the item you want.

@Html.Sitecore().Field("Title", Item)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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