November Happy Hour will be moved to Thursday December 5th.

Map to Nested List Object in ViewModel

Vote:
 

I have a complex JSON object with nested lists and nesteded values. Traditionally in a MVC environment I can use List Model binding which allows me to map to a list nested within the object. Below is an example of how it would be done in traditional MVC within a single Model file.

public class StudentRush : PageData
{
public virtual ContentArea Form { get; set; }
public virtual ContentArea FormSubmit { get; set; }
public virtual List<ElectronicAddress> ElectronicAddresses { get; set; }
}

public class ElectronicAddress
{
public virtual string Address { get; set; }
public virtual object Id{ get; set; }
public virtual bool IsFromAffiliation { get; set; }
}

When I try this in Episerver I get the error Type...could not be mapped to a PropertyDefinitionType. This is the case even when I use IList. Does anyone know how I can achieve model binding without straying too much away from the traditional method?

Thanks

#202964
Apr 04, 2019 16:11
Vote:
 

Hi,

Using custom complex objects as properties on a page model cant be done by default. Depending on what you want to achieve with the list I would suggest either creating a custom property definition or setting up a selectionfactory.

#202966
Apr 04, 2019 16:44
Vote:
 

Episerver CMS properties have to be able to load from/save to primitive types (datetime, int, decimal, etc.). In cases of complex types, you need to define a backing type to let the system knows how to save that property to a LongString one, and how to read from it (it does not have to be LongString, of course, but that is the most common backing type). I think the links provided by Gustav are good starting point. 

#202968
Apr 04, 2019 16:57
Vote:
 

Hi Gabe,

What you want to do should be possible with a PropertyList<T> although using a ContentArea and blocks is typically recommended.

What you're missing is the PropertyDefinitionTypePlugIn, have a look at the documentation: https://world.episerver.com/documentation/developer-guides/CMS/Content/Properties/generic-propertylist/.

#202981
Edited, Apr 04, 2019 21:25
Vote:
 

Hey all,

Thanks for all of the help it's much appreciated.

I realize that I'm working in an Episerver 7 environment and the feature that Jake Jones is referring to is only avaible in Episerver version 9+. I will work with my  team on updating our CMS version then attempt to implement the `PropertList<T>` method.

Best,

Gabe

#203126
Apr 09, 2019 22:08
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.