1

I'm using @Html.Glass().Editable(Model, x => x.MyProperty) for some of our single-text / rich text fields. When I load the site with this code in the view everything looks fine. However, when I open the Experience Editor I see this error:

Value cannot be null. Parameter name: objectToSwitchTo.

   at Sitecore.Diagnostics.Assert.ArgumentNotNull(Object argument, String argumentName)
   at Sitecore.Common.Switcher`2.Enter(TValue objectToSwitchTo)
   at Glass.Mapper.Sc.GlassHtml.MakeEditable[T](Expression`1 field, Expression`1 standardOutput, T model, Object parameters, Context context, Database database, TextWriter writer)

I've seen a few other SO threads about this but no real solutions for me. I'm using a data source set on the rendering. I've stepped through the entire process and don't see any null attributes in my model after using GetDataSourceItem<MyClass>. However, one thing I noticed is that my Sitecore ID field is all zeroes. I saw a different error prior to this, saying I needed to add a Sitecore ID field to my model, so I did. My code for the ID field is as follows:

    [SitecoreId]
    public virtual Guid Id { get; }

My Glass version is 4.5.0.4 which I believe is still pretty recent.

3
  • If I change my definition of the Id field to { get; set; }, it works...
    – Paul
    Commented Oct 3, 2018 at 22:29
  • 2
    You need a setter for the Id field so Glass can map your model back to Sitecore Item. From what I remember, the setter doesn't have to be public. [SitecoreId] public virtual Guid Id { get; private set; } should do the trick.
    – Marek Musielak
    Commented Oct 4, 2018 at 5:50
  • It's funny because I tried private set first and it still didn't work. Regardless, it seems like I've found a solution to the problem - using { get; set; }.
    – Paul
    Commented Oct 4, 2018 at 21:54

1 Answer 1

2

The fact I was getting an empty ID made me think that maybe Glass wasn't able to set the item ID on the property, so I changed my field definition from:

[SitecoreId]
public virtual Guid Id { get; }

to:

[SitecoreId]
public virtual Guid Id { get; set; }

Now things are working fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.