Reputation: 1320
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.
Upvotes: 1
Views: 4772
Reputation: 2635
If your controller is derived from GlassController
you can use GetDataSourceItem<I..>()
. That will give you the datasource.
Please note: Glassmapper is now absolute. This class will be removed in Future Releases. CS0618 Warning.
Upvotes: 3
Reputation: 6298
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);
}
}
Upvotes: 2
Reputation: 1879
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.
Upvotes: 0