4

I'm using Glass V4. I have a set up of MVC Web Area Project.

I have installed the Glass Mapper in the Main Project (WebProject).

I'm trying to do the Glass Casting in my Area Project.

 public class ContactController : SitecoreController
{
    private readonly ISitecoreContext _context;
    private IGlassHtml _glassHtml;

    public ContactController()
        : this(new SitecoreContext())
    {

    }
    public ContactController(ISitecoreContext context)
    {
        _context = context;
        _glassHtml = new GlassHtml(context);

    }

    // GET: Contact
    public ActionResult ContactUs()
    {
        var db = Sitecore.Context.Database;
        var datasource = db.GetItem(RenderingContext.Current.Rendering.DataSource);

        var ViewModel = new Models.ContactUs();
        ViewModel.Headerstring = datasource.Fields["Headerstring"].Value;
        ViewModel.Substring = datasource.Fields["Substring"].Value;
        ViewModel.Description = ((MultilistField)datasource.Fields["Description"]).GetItems().Select(s => s.Fields["Line"].Value).ToList<string>();

        return View(ViewModel);
    }

    public ActionResult ContactUsGlass()
    {
        var model = _context.GetCurrentItem<ContactUsGlassModel>();
        return View(model);
    }
}

I'm able to get the value with the First Action Method but not with the second.

Model:

public class ContactUs
{
    public string Headerstring { get; set; }
    public string Substring { get; set; }
    public List<string> Description { get; set; }
}

Glass Model:

public class ContactUsGlassModel
{
    public virtual string Headerstring { get; set; }
    public virtual string Substring { get; set; }
}

I understand I don't need to register my Namespace in Glass V4.

Marek Musielak
  • 26,832
  • 8
  • 72
  • 80
Sakthivel
  • 553
  • 1
  • 5
  • 12

3 Answers3

5

You should not use _context.GetCurrentItem method. Use _context.GetItem instead:

public ActionResult ContactUsGlass()
{
    var model = context.GetItem<ContactUsGlassModel>(RenderingContext.Current.Rendering.DataSource);
    return View(model);
}

You don't want to get model from your Sitecore.Context.Item (which is used in GetCurrentItem method. You want to get your model from the DataSource of the current rendering.

Marek Musielak
  • 26,832
  • 8
  • 72
  • 80
  • I have updated the code, var model = _context.GetItem(RenderingContext.Current.Rendering.DataSource); But now I'm unable to unittest the code due to RenderingContext. can you please advise on this. – Sakthivel Nov 08 '15 at 04:32
  • @Sakthivel It's worth taking a read of [Life Through a Lens – Unit Testing with Glass Controllers](http://cardinalcore.co.uk/2015/09/29/life-through-a-lens-unit-testing-with-glass-controllers/) – jammykam Nov 08 '15 at 09:47
0

What @Marek has answered is the right way of pulling the rendering item into model. GetCurrentItem by default gives the page item being served by Sitecore. If the fields that your model needs are fields of your page item then GetCurrentItem can also fill your model. If Datasource nesting is enabled, then if the datasource is not set on the rendering, Sitecore returns the page item again.

phani
  • 161
  • 9
  • @Sakthivel, you can pull an IRenderingContext interface from RenderingContext class. Create an MVC model binder for IRenderingContext and register it. I usually do this pattern for most of the Sitecore.Context related properties. Also worth noting is this pattern for GlassModelBinder. Though not the same way, I use similar approach in my solutions [link](https://github.com/BenGGolden/GlassModelBinding) – phani Nov 08 '15 at 17:07
  • Sure will try this one too... Thanks...! – Sakthivel Nov 08 '15 at 19:32
0

You can inherit from GlassController and then use GetLayoutItem() to get the datasorced item. If it's null then you need to publish the template in sitecore and make sure you mappings are correct if you are not using TDS :)