Try our conversational search powered by Generative AI!

Custom date range Criterion using dijit.form.DateTextBox doesn't load properly after saving

Vote:
 

Hi,

I have created a custom Date Range criterion to allow for visitor group personalization based on time of year (think seasonal campaigns).

The criterion and visitor group are quite simple, just a Start Date and End Date to take effect:

    public class DateRangeCriterion : CriterionModelBase
    {
        [Required(ErrorMessage = "Start Date is required")]
        [DojoWidget(WidgetType = "dijit.form.DateTextBox")]
        public string StartDate { get; set; }

        [Required(ErrorMessage = "End Date is required")]
        [DojoWidget(WidgetType = "dijit.form.DateTextBox")]
        public string EndDate { get; set; }
        
        public override ICriterionModel Copy()
        {
            // if this class has reference types that require deep copying, then
            // that implementation belongs here. Otherwise, you can just rely on
            // shallow copy from the base class
            return base.ShallowCopy();
        }

        public override string ToString()
        {
            return $"[{this.GetType()}; StartDate={StartDate}; EndDate={EndDate}]";
        }
    }

    [VisitorGroupCriterion(
        Category = "Time and Place Criteria",
        Description = "Allows you to select a start and end date for a particular visitor group.",
        DisplayName = "Date Range")]
    public class DateRangeVisitorGroup : CriterionBase<DateRangeCriterion>
    {
        public override bool IsMatch(IPrincipal principal, HttpContextBase httpContext)
        {
            try
            {
                var startDate = DateTime.Parse(Model.StartDate).ToUniversalTime();
                var endDate = DateTime.Parse(Model.EndDate).ToUniversalTime();

                if (DateTime.UtcNow >= startDate && DateTime.UtcNow <= endDate)
                    return true;
            }
            catch (Exception e)
            {
                var log = LogManager.GetLogger();
                log.Error($"Unable to evaluate Visitor Group criteria for {this.GetType()}, exception occurred parsing Model: {Model}");
            }

            return false;
        }
    }

When I compile this into the solution, I can see that the criteria is available and it does work as anticipated.  The diji.form.DateTextBox formats the two inputs as calendar date pickers which is quite nice for the content administrator.

However, after creating one of these Criteria, saving it, and then coming back later to edit it- the StartDate and EndDate fields are "empty" on the front end. When I remove the DateTextBox dojo widget type, I see the raw string correctly on the text input.  It appears that the DateTextBox dojo type cannot initialize properly with the saved data.  Are there any options I can pass into the DateTextBox dojo widget to get this to behave properly?

#256181
Jun 07, 2021 16:59
Vote:
 

Can you change the DataRangeCriterion to  public DateTime StartDate { get; set; } instead? I am wondering if the Dojo widget requires/parses a datetime?

#256221
Jun 08, 2021 7:54
Dylan McCurry - Jun 10, 2021 13:45
Hey David, when I tried this I got a 500 error :(

Object of type 'System.String' cannot be converted to type 'System.DateTime'.
* 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.