How to Implement Sitecore MVC View Rendering with RenderingModel

In a Sitecore MVC project, if there is no complicated business logic and just display Sitecore content directly from data source, we’d prefer to create a view rendering instead of additional controller and even model. Here is a simple way with Sitecore built-in RenderingModel.

In the MVC project, create a partial view with directive Sitecore.Mvc.Presentation.RenderingModel. Sitecore automatically passes an instance of the Sitecore.Mvc.Presentation.RenderingModel class to the view, we can access two main properties of interest: Item and PageItem. Item refers to the datasource item set for the view in the rendering component properties, whereas PageItem refers to the context item of the page, such as parameters.

14-08-2015 3-41-21 PM

In order to make field editable by Experience Editor, we should use “Html.Sitecore().Field()” method. But if some of the fields are to be disable, for example page title,  matadata, etc, we can set it through parameter, new { DisableWebEdit = true }. To make it simple, let’s make an extension of SitecoreHelper called SitecoreFieldHelper.

14-08-2015 4-02-25 PM

Use

@Html.Sitecore().ImageField("Picture", Model.Item, 250, 300, 10, 5, true)

replace

@Html.Sitecore().Field("Picture", Model.Item, new {mw=250, mh=300, hspace = 10, vspace = 5, DisableWebEdit=true})

In Sitecore, create a view rendering with path pointing to this partial view. There is no model required. Here is how it looks in Experience Editor.

14-08-2015 3-44-08 PM

 

Here you can see how easy it is to create a custom rendering in MVC to display Sitecore content from item datasource.

This entry was posted in Information Technology, MVC, Sitecore and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s