Sakthivel
Sakthivel

Reputation: 553

Why Glass Mapper Returning Null Values?

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.

Upvotes: 4

Views: 3167

Answers (3)

Charlie Afford
Charlie Afford

Reputation: 86

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 :)

Upvotes: 0

phani
phani

Reputation: 161

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.

Upvotes: 0

Marek Musielak
Marek Musielak

Reputation: 27142

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.

Upvotes: 5

Related Questions