Default Values for Fields

Topics: Writing modules
Feb 17, 2013 at 10:10 PM
I am trying to set default values for a DateTime Field in the driver but it does not seem to work. Is there a way to set default values for Fields (such as DateTimeFiled)?

Here is the code I was using:
 protected override DriverResult Editor(AdvertisementPart part, dynamic shapeHelper) {
            dynamic dynPart = part.ContentItem;
            var startDate = dynPart.AdvertisementPart.StartTime;
            var endDate = dynPart.AdvertisementPart.EndTime;

            if (startDate.DateTime.Equals(DateTime.MinValue)) {
                dynPart.AdvertisementPart.StartTime.DateTime = DateTime.Now.ToUniversalTime();
            if (endDate.DateTime.Equals(DateTime.MinValue)) {
                dynPart.AdvertisementPart.EndTime.DateTime = DateTime.Now.AddYears(1).ToUniversalTime();

            return ContentShape("Parts_Advertisement_Edit", () =>
                                                        TemplateName: "Parts/Advertisement",
                                                        Model: part,
                                                        Prefix: Prefix));
Feb 17, 2013 at 10:42 PM
Did you try to attach a debugger and set some breakpoints in there in order to figure out what's going on?
Feb 18, 2013 at 12:56 AM
I did attached a debugger, the value is set on the part that is passed into return (I will grab a screen shot and attach when I get back to my dev machine). However, upon render the 2 attached fields display no value (The hope was start and end times would be pre-populated), I also tried to attached to the specific field view, but was unable to get it to break in (assuming I looking at the wrong place/view). I worked around the issue by making both start and end date required and checking that end time is greater than start time (But pre-populating with default values is a better solution). This worked w/o issue.
Feb 18, 2013 at 3:33 AM
Here are pictures of what I am seeing:
In Debugger:

Just In case this is the migration Code for the Part
 SchemaBuilder.CreateTable("AdvertisementPartRecord", table => table

            ContentDefinitionManager.AlterPartDefinition("AdvertisementPart", builder => builder
                 .WithField("Image", field => field
                     .WithSetting("MediaPickerFieldSettings.Required", true.ToString(CultureInfo.InvariantCulture))
                     .WithSetting("MediaPickerFieldSettings.AllowedExtensions", "jpg png")
                     .WithSetting("MediaPickerFieldSettings.Hint", "Select advertisement image.")
                     .WithDisplayName("Advertisement Image"))
                 .WithField("StartTime", field => field
                    .WithSetting("DateTimeFieldSettings.Display", "DateAndTime")
                    .WithSetting("DateTimeFieldSettings.Required", true.ToString(CultureInfo.InvariantCulture))
                    .WithSetting("DateTimeFieldSettings.Hint", "Time when the advertisement begins")
                    .WithDisplayName("Start Time"))
                 .WithField("EndTime", field => field
                    .WithSetting("DateTimeFieldSettings.Display", "DateAndTime")
                    .WithSetting("DateTimeFieldSettings.Required", true.ToString(CultureInfo.InvariantCulture))
                    .WithSetting("DateTimeFieldSettings.Hint", "Time when the advertisement ends.  Must be greater than Start Time.")
                    .WithDisplayName("End Time")));

            ContentDefinitionManager.AlterTypeDefinition("Advertisement", builder => builder
Feb 19, 2013 at 4:14 PM
I did a lot of debugging, it appears that what is happening is the DateTimePicker field is setting a view model before it is in my driver code. The line dynPart.AdvertisementPart.EndTime.DateTime = DateTime.Now.AddYears is setting the parts DateTime field but too late for it to be passedinto the ViewModel that is being used by DateTimeField view. I looked to see if I could find the model in the part object tree, but was unsuccessful.

Workaround I came up with
-Change AdvertisementPart edit to use a view model and pass down default values to be set. Then in the view I use JQuery to set the text boxes that make up the datetime picker to the default values as needed.