0

I am upgrading Glapp Mapper to V5. I see that the GlassView is deprecated and instead we have to use @Html.Glass().

In V4, my code has something like @Editable(Model, x=>x.BackGroundColor)

So I changed my code to something like @Html.Glass().Editable(Model, x=>x.BackGroundColor).

When I open Experience editor, This piece of code was throwing an error. Failed item resolve - You cannot save a class that does not contain a property that represents the item ID. Ensure that at least one property has been marked to contain the Sitecore ID. Type: <NameOfMyClass>. After looking at the below link,

Glassmapper | Cannot save a class that does not contain a property that represents the item ID

I have added an ID to my model

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

Now the above error went away, instead got a new error Value cannot be null. Parameter name: objectToSwitchTo.
I saw that this question was asked in the past as in below link
Glass Error opening Experience Editor (ObjectToSwitchTo)

It already explains that if we have set method with virtual keyword, it should work. But in my case I already have those implemented and still having the null error.

I also tried assigning a value to Id something like id = Guid.NewGuid(); which has generated a new GUID. but nothing seems to work. Any thoughts?

Here is my Model

public class TestViewModel  
    {  
        #region Master Properties  
        public FundViewModel FundVM { get; set; }  
        public BaseViewModel BaseVM { get; set; } 
 
        [SitecoreId]   
        public virtual Guid Id { get; set; }    
        #endregion  

        public virtual SitecoreFields Fields { get; set; }  

        public class SitecoreFields  
        {            
            public virtual string Title { get; set; }  
            public virtual string BackgroundColor { get; set; }
         }  

     }
7
  • Could you please share your model code here? make sure you have all the properties as virtual. Dec 16, 2021 at 15:58
  • @MahendraShekhawat: I have added my ViewModel code in description Dec 17, 2021 at 1:15
  • Please move the public virtual Guid Id { get; set; } into SitecoreFields and try Dec 17, 2021 at 8:35
  • @MahendraShekhawat: I tried but no good news. The same Value cannot be null. Parameter name: objectToSwitchTo error. Dec 17, 2021 at 9:19
  • just want to make sure your field name is not having space correct? its same as property name Dec 17, 2021 at 13:06

1 Answer 1

0

With no good news with my trials, we have decided to refactor our models to just pass through the native Glass objects. We simplified the View Model to pass the native objects, instead of the indirect hand-off to a custom SitecoreFields type. So, this bypassed the problem.

public class TestViewModel  
    {  
        #region Master Properties  
        public FundViewModel FundVM { get; set; }  
        public BaseViewModel BaseVM { get; set; } 
        #endregion  
        
        public ISiteSettings SiteSettings { get; set; }
     }

and added [SitecoreId] for Guid Id in a partial class as shown below.

public abstract partial class SitecoreItem : ISitecoreItem
    {
        [SitecoreId]
        public virtual Guid Id { get; set; }
}

The above change worked for us and we were able to resolve this.

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.