Try our conversational search powered by Generative AI!

Default value for properties on already created pages?

Vote:
 

If I would use the SetDefaultValues method of the PageData object to give my properties a default value it would only affect pages that are created after this change.

I understand that if episerver would change properties on previously created pages that would be a big risk and I don't mean to change this.

What I would like to do however is to return a fallback value from the model if the property is not set.

Lets say I have this new property

        [Display(Name = "Max antal tecken i brödtext för sökresultat", GroupName = SystemTabNames.Content, Order = 106)]
        public virtual int ExcerptLength { get; set; }

This property would be the text limit of the excerpt in a search result. Since the search page is already created this would just result in the property returning 0 even if i would set it to 200 in SetDefaultValue method.

Is there any way to return a value that is not based on the property value if the property is not set? I still want to access the property CurrentPage.ExcerptLength to retrieve this. What I mean to say is that I don't want to handle the fallback value in controller or any extra read-only property on the model.

Best regards

Andreas

#91013
Sep 24, 2014 10:31
Vote:
 

Hi!

You can create custom getters for your model properties that implements default values. There is an example of this in the Alloy templates where a default image is returned if no image has been selected.

#91025
Sep 24, 2014 12:27
Vote:
 

Thank you. So simple. Dont know why I didn't figure this out myself hehe.

For others reading. This is the code from alloy:

        [DefaultDragAndDropTarget]
        [UIHint(UIHint.Image)]
        public virtual Url Url
        {
            get
            {
                var url = this.GetPropertyValue(b => b.Url);

                return url == null || url.IsEmpty()
                           ? new Url("/Static/gfx/logotype.png")
                           : url;
            }
            set
            {
                this.SetPropertyValue(b => b.Url, value);
            }
        }
#91030
Sep 24, 2014 13:05
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.