This project is read-only.

Setting default values for ContentPart properties

Topics: Writing modules
Dec 15, 2014 at 10:52 AM
I have a module that offers a ContentPart that can be attached to a ContentItem.

Because you might not want to display it every time I've added a "Show Item" bool to it which it can be used to toggle it on or off on a per ContentItem basis.

I can't figure out how to set a default value as I want it to be ticked (shown) by default.

I tried updating the Migrations.cs WithDefault, which it sets the default in the SQL but doesn't seem to apply it when displayed in the admin panel eg:
            SchemaBuilder.CreateTable("StaticMapPartRecord", table => table
                .Column("Visible", DbType.Boolean, c => c.WithDefault(true))
                // ... snip ...
And I've tried using DataAnnotations on the ContentPart model definition:
using System.ComponentModel.DataAnnotations;

namespace rtpDesign.StaticMap.Models
    public class StaticMapPart : ContentPart<StaticMapPartRecord>
        [DisplayName("Show Static Map?")]
        public bool Visible
            get { return Retrieve(r => r.Visible); }
            set { Store(r => r.Visible, value); }

        // ...snip...
I've also tried searching through the Orchard codebase to see if there are any examples but have come up empty - has anyone tackled this one?
Jan 22, 2015 at 1:51 PM
I have been searching for something similar and found this blog post -

I've not tried it as I'm actually searching for how it could be possible to set defaults via the admin UI, but I thought it might be useful for you/others