Try our conversational search powered by Generative AI!

DefaultValue not working

Vote:
 

Hello everyone

In a Block a [DefaultValue] is not working. It is an existing Block, existing properties [DefaultValue] is working fine. I added a new property and it won't set the default value. Am I missing something?

This is the new property within the Block

[Display(Order = 20, GroupName = "Image d'arrière-plan", Name = "Safezone", Description = "La grandeur en px ou % de la safezone à partir de la droite")]
[DefaultValue("33%")]
public virtual string SafezoneImage { get; set; }



#198284
Oct 24, 2018 14:58
Vote:
 

Hello Martin

Have you tried using the SetDefaultValues method which is the recommended way of setting default values?

https://world.episerver.com/documentation/class-library/?documentId=cms/7/d07dd3aa-1b79-de9e-3a67-4444e17ebe32 

David

#198286
Oct 24, 2018 15:45
Vote:
 

Yes, following is the overriden method. It's working for some fields. Any ideas?

        public override void SetDefaultValues(ContentType contentType)
        {
            var memberInfo = GetType().BaseType;
            if (memberInfo != null)
            {
                var properties = memberInfo.GetProperties();
                foreach (var property in properties)
                {
                    var defaultValueAttribute = property.GetAttribute<DefaultValueAttribute>();
                    if (defaultValueAttribute != null)
                    {
                        this[property.Name] = defaultValueAttribute.Value;
                    }
                }
            }
            base.SetDefaultValues(contentType);
        }
#198287
Oct 24, 2018 16:01
Vote:
 

Maybe its a code style thing but I always implement the base implementation before my own when overriding methods. Its a guess but perhaps something in the base.SetDefaultValues method may be doing something to override your implementation? Have you been able to debug and actually see the value getting set?

#198290
Oct 24, 2018 16:13
Vote:
 

Strangely it is working when creating new pages. Unfortunately, when going under EPI Server / Admin / Content Type / Edit Safezone property of my Block the default value doesn't appear.

#198295
Oct 24, 2018 19:46
Vote:
 

As I understand it, the DefaultValue attribute and the SetDefaultValues method only applies values when first creating the content. It's not intended for setting values on existing content when their content types get updated or ensuring that already created content gets updated.

An option is to tap into the OnSaving event and set the values from there if needed.

Something like this:

    [ModuleDependency()]
    public class ContentEvents : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            var contentEvents = context.Locate.ContentEvents();

            contentEvents.SavingContent += SetDefaultValues;
        }

        public void SetDefaultValues(object sender, ContentEventArgs e)
        {
            // Set your defaults here if they're not set.
            var startPage = e.Content as StartPage;
            if (startPage != null)
            {
                if (string.IsNullOrEmpty(startPage.Title))
                    startPage.Title = "Hello!";
            }
        }

        public void Uninitialize(InitializationEngine context)
        {
        }
    }
#198298
Edited, Oct 24, 2018 23:36
Vote:
 

You're not missing anything Martin. There's a bug in admin UI which doesn't display default values that are defined from the code.

#198314
Oct 25, 2018 8:47
Vote:
 

Has it been a bug for long? I remember wanting to do something similar to Martin a year and a half ago and back then it did not work with SetDefaultValues either.

#198316
Edited, Oct 25, 2018 10:16
Vote:
 

Hello Martin

I think I misunderstood your original requirement. You are adding the attribute to an existing instance of a block and you are expecting the default value to be set? If thats the case then thats now how it works. Jafet has explained this and suggested a work around.

It's also summarised in the comments that originally proposed the [Default] attribute based way of setting values:

"IMO, I prefer to set these values in one place as you override the SetDefaultValues function, it applies to the new page instance only once. If you make them as attribute, it will mislead the other developers as they may try to use this to alter the property's value."

https://world.episerver.com/blogs/Per-Magne-Skuseth/Dates/2014/3/Attribute-based-default-values/ 

David 

#198317
Oct 25, 2018 10:45
Vote:
 

I misunderstood it too. :)

But for claritys sake, for future visitors, the bug is that if you have defined a default value for a content type property like so:

It will work with setting that value when you create such content types, but it won't be displayed here:

#198364
Edited, Oct 26, 2018 1:49
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.