Try our conversational search powered by Generative AI!

PropertyList with string collection losing data when editing entry

Vote:
 

Hi,

We need to store information about location into the property which is something like country + X cities. I thought it would be great to use for that PropertyList, so defined class for location like:

    public class Location
    {
        public string Country { get; set; }

        public IList Cities { get; set; }
    }



after that added to my page type property:

        [CultureSpecific]
        [Display(Order = 40)]
        [EditorDescriptor(EditorDescriptorType = typeof(CollectionEditorDescriptor))]
        public virtual IList Locations { get; set; }



and also added class:

    [PropertyDefinitionTypePlugIn]
    public class LocationListProperty : PropertyList
    {

    }



As an effect I got nice looking property for locations https://www.screencast.com/t/oGQhIrMitt

but the issue is when you click edit on single row the values from Cities list are lost https://www.screencast.com/t/9LIQcTr3WJN

Do I have something missing still to make it working? or is the PropertyList not designed to use collections in the class?

I assumed that do not need anymore to do serializer tricks which were needed in earlier Episerver (when it was Beta version) explained here https://world.episerver.com/blogs/Per-Magne-Skuseth/Dates/2015/11/trying-out-propertylistt/

#189311
Mar 15, 2018 10:32
Vote:
 

you might need to put a json converter on type of your list property for cities.  I ran into this with the url property where when you save, the values would jsut disappear.  Try putting a JsonConverter(typeof(xxx)) on type of your Cities properties.

#189375
Mar 16, 2018 15:02
Vote:
 

Hi Joshua,

I have tried what you suggested, implemented the converter based on that implementation https://blog.bitscry.com/2017/08/31/single-or-array-json-converter/ and then decorated my property like:

        [JsonConverter(typeof(SingleOrArrayConverter<string>))]
        public IList<string> Cities { get; set; }

But still no luck and same behavior.

#189391
Edited, Mar 16, 2018 23:14
Vote:
 

Greg, I tried this with Alloy site. Changing the editor for the Cities list to the Alloy StringListEditorDescriptor gets a "working" edit but naturally it is displaying the textarea editor where you put the "city name" on a separate line.

[EditorDescriptor(EditorDescriptorType = typeof(Business.EditorDescriptors.StringListEditorDescriptor))]
public IList<string> Cities { get; set; } = new List<string>();

It isn't what you are trying to achieve, but maybe a hint that you should create own editor to make it work. Maybe the editor what you get is just a side effect of something.

I tested also with changinig the IList<string> to ICollection<string> and then the default editor only showed properties for the ICollection: Count and IsReadOnly

#189392
Mar 17, 2018 0:33
Vote:
 

Hi Antti, if you are talking about having that property with no use of PropertyList it works nicely then you don't even need any descriptor with IList<string> properties, but here in connection with PropertyList it fails on edit data.

It might be that editor I get now is kind of side effect of something else.

#189394
Mar 17, 2018 3:51
Vote:
 

Hi Greg, no I meant that you only use the stringlisteditor from Alloy and decorate your Cities property with as shown above code. You still have your Locations and the same code that you originally posted. You just change the editor for the Cities list. UI works ok and you can edit existing cities list. The only downside is that the editor for the cities list is now textarea where you write each city on a separate line. In your code you can still do foreach to the locations list and foreach for the cities list.

I also tested with changing the Cities items to custom object like:

public class City
{
    public string Name { get; set; }
}
[EditorDescriptor(EditorDescriptorType = typeof(CollectionEditorDescriptor<City>))]
public IList<City> Cities { get; set; } = new List<City>();

This also work, only downside is the editor experience when editing the items in cities. Also when looking the all properties view of the page having the locations property, the editor displays the locations property something like this:

Finland [object Object], [object Object]
Sweden [object Object]
#189395
Mar 17, 2018 9:31
Vote:
 

Alright, I will then try to get the StringListEditorDescriptor from Alloy and decorate Cities property. The fact it is textarea is still acceptable, since I assume in most of the cases, pages will be handled anyway only by scheduled job.

As long I can iterate locations and cities from code it is fine. I wanted to avoid idea of creating separate block types for locations since it requires more complex logic to use later the data from it and PropertyList looked much more friendly in that case.

Will let you know if it worked when I find some time during weekend, thanks for now!

#189396
Mar 17, 2018 9:55
Vote:
 

Hi Antti, it works! Please take the credit :)

As you said the only negative thing it is showing cities as textarea, each value in separate line, but I would say it is acceptable in that case.

However I would like to hear from someone from Epi core team if that is the way or we missed some other recommended approach.

#189403
Mar 18, 2018 19:06
Vote:
 

Greg, maybe it would be good idea to create a developer ticket about this. Maybe there is currently a bug and it will get fixed or maybe a feature the development team can easily implement.

#189409
Mar 19, 2018 6:25
Vote:
 

I have created support ticket as well, will keep you informed how it ended

#189431
Mar 19, 2018 13:20
Vote:
 

Got an answer to my ticket:

Hi Grzegorz,

The PropertyList was designed primarily to be used with simple types such as int, string, etc and the “out-of-the-box experience” will only cater for such types. If you want to use a more complex type as described in the forum thread, both responsible for making sure that the type can be serialized (using Json.NET) and that it’s properly decorated for editing. I hope this will clarify your concern.

Thanks,
Trung
Support Team
#189585
Mar 21, 2018 7:37
Vote:
 

We have already fixed that issue. It will be released in 11.4.2 or 11.5.0.

#189647
Edited, Mar 22, 2018 9:14
Vote:
 

Thanks a lot Bartek!

Looking forward for the release, any kind of estimate when it will come?

#189650
Mar 22, 2018 9:25
Vote:
 

And Bartosz one more thing, what will be the official recommended way how to handle such cases? do we really need still to have that SingleOrArrayConverter class with decorating property with the attributes like

    public class Location
    {
        public string Country { get; set; }
        
        [JsonProperty]
        [JsonConverter(typeof(SingleOrArrayConverter<string>))]
        public IList<string> Cities { get; set; }
    }



or this is not needed anymore with your fix?

Btw 11.5 has been already released and I have still that issue on Alloy with latest

Ah or actually you are probably saying about EPiServer.CMS.UI package version release

#189653
Edited, Mar 22, 2018 9:48
Vote:
 

Yes, sorry I should have been more specific. I meant EPiServer.CMS.UI - I'm from the UI team hence the misunderstanding :)

About your question - you don't need any attributes.

public class Location
{
    public string Country { get; set; }          
    public IList<string> Cities { get; set; }
}

[PropertyDefinitionTypePlugIn]
public class LocationListProperty : PropertyList<Location>
{
}

and then

[EditorDescriptor(EditorDescriptorType = typeof(CollectionEditorDescriptor<Location>))]
public virtual IList<Location> Locations { get; set; }

in your page model would work fine.

#189661
Edited, Mar 22, 2018 11:13
Vote:
 

Awesome, I thought so we do not need any extra stuff since I got the same result already with the code you posted above.

Looking forward to see the fix in next release (hope so) and right after that you will get your answer marked as solution ;)

thanks (dzięki), Greg

#189664
Mar 22, 2018 12:42
Vote:
 

Fix has been released in update 208 https://world.episerver.com/releases/episerver---update-208/

Thanks once again Bartosz!

#190167
Apr 03, 2018 22:29
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.