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?
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:
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?