Enumerable.Empty<ObjectSelectorItem>() returning null in wdget properties

Rory Aherne asked on December 13, 2023 17:01


I'm on v13.0.124

I don't have the issue for instantiating an empty collection of PathSelectorItem but I do for ObjectSelectorItem. I also tried setting it as a new List but that also returns null

//this works and returns and empty collection
[EditingComponent(PathSelector.IDENTIFIER, Order = 1, Label = "Path")]
[EditingComponentProperty(nameof(PathSelectorProperties.MaxPagesLimit), 1)]
public IEnumerable<PathSelectorItem> PathSelectorItems { get; set; } =  Enumerable.Empty<PathSelectorItem>();

//This doesn't work and returns null
[EditingComponent(ObjectSelector.IDENTIFIER, Order = 3, Label = "Categories")]
[EditingComponentProperty(nameof(ObjectSelectorProperties.ObjectType), "cms.category")]
[EditingComponentProperty(nameof(ObjectSelectorProperties.MaxItemsLimit), 3)]
public IEnumerable<ObjectSelectorItem> CategoriesSelector { get; set; } = Enumerable.Empty<ObjectSelectorItem>();

Am I doing something wrong?


Recent Answers

Jono Rickard answered on December 14, 2023 22:30

Enumerable.Empty wont ever return null, so the null CategoriesSelector your seeing means that field has been updated after your example line.

I'd say this is due to kentico setting it to null after pulling the widget property data from the database.

Might be worth just null checking the property before trying to access it?

Or also, just a thought - have you tried applying the [Required] attribute to it like PathSelectorItems?

0 votesVote for this answer Mark as a Correct answer

Rory Aherne answered on December 18, 2023 10:58

@Jono Rickard you're definitely on to something there with the required attribute.

I can't add it to the categories setting as it's not required and adding it for the sake of debugging doesn't work as I can't submit it as empty.

However, I noticed I can submit the settings with the Path selector empty.

Seems to be a kentico bug

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.