1
votes

need an suggestion on Sitecore rendering where i want to use glassmodel to render the item.

I have a page and that has many renderings and each renderings has data source associated with it.

I know that below statement will give me the Current context, not the datasource item.

var context = new SitecoreContext();
            Model = context.GetCurrentItem<HomePage>();

What is the best option to solve my query? I gave gone through this article. But i'm really not convinced with that method as i'm using IoC (Windsor Castle) and have to write Unit test cases for each method. I may have to mock these objects later. I'm looking for the approach where i will be using Interface not the class.

Appreciate your help.

3

3 Answers

3
votes

If your controller is derived from GlassController you can use GetDataSourceItem<I..>(). That will give you the datasource.

1
votes

If you using Glass Mapper 5, the documentation doesn't refer to using GlassController anymore. You can use IMvcContext like below:

using System.Web.Mvc;
using Glass.Mapper.Sc.Web.Mvc;

public class TitlesController : Controller
{
    public ActionResult Index()
    {
        IMvcContext mvcContext = new MvcContext();

        // will return the DatasourceItem if set, otherwise the page context is returned
        Titles model = mvcContext.GetRenderingItem<Titles>();

        Titles dsModel = mvcContext.GetDataSourceItem<Titles>();

        return View(model);
    }
}
0
votes

Adding to Gatogorodo's answer

if you controller is derived from GlassController or if you are in GlassView you can use this.DatasourceItem to get the data source.

this.GetDataSourceItem<Model>() will get you the desired model which might be one of the base template for the item template.