Convert Sitecore item into RenderingModel for MVC

adminHabitat, Sitecore, Sitecore MVC2 Comments

Following code helps to add in item extension class or helper class to convert Sitecore item into Sitecore.Mvc.Presentation.RenderingModel. I have added code and extended in local Habitat Foundation project.

        public static RenderingModel AsRenderingModel(this Item item)
        {
            RenderingContext renderingContext = RenderingContext.CurrentOrNull;
            Assert.IsNotNull(renderingContext, "RendreingContext is null");

            // get rendering
            var rendering = renderingContext.Rendering;
            Assert.IsNotNull(rendering, "Rendreing is null");

            // set the current rendering item
            rendering.Item = item;

            // create and intialise the rendering model
            RenderingModel renderingModel = new RenderingModel();
            renderingModel.Initialize(rendering);
            
            // return the rendering model
            return renderingModel;
        }


Let me know if you have any trouble.

Cheers

2 Comments on “Convert Sitecore item into RenderingModel for MVC”

  1. Pretty section of content. I just stumbled upon you site and iin accession capital to assert that I get in fact enjoyed account your
    blog posts. Any way I’ll be subscribing to your augment and even I achievement
    you access consistently rapidly.
    www forlap dikti go id perguruan tinggi

Leave a Reply

Your email address will not be published. Required fields are marked *