1
votes

I'm following the example here: http://demos.kendoui.com/web/datepicker/rangeselection.html

Here is how I'm defining my fields inside of an EditorTemplate:

<div class="editor-label">
    @Html.RequiredLabelFor(model => model.StartDate)
</div>
<div class="editor-field">
    @Html.Kendo().DatePickerFor(model => model.StartDate).Value(DateTime.Now).Max(DateTime.Now.AddDays(2)).Events(e => e.Change("startChange"))
    @Html.ValidationMessageFor(model => model.StartDate)
</div>

<div class="editor-label">
    @Html.RequiredLabelFor(model => model.EndDate)
</div>
<div class="editor-field">
    @Html.Kendo().DatePickerFor(model => model.EndDate).Value(DateTime.Now.AddDays(2)).Min(DateTime.Now).Events(e => e.Change("endChange"))
    @Html.ValidationMessageFor(model => model.EndDate)
</div>

I am using the EditorTemplate within a KendoUI Grid that has a create button that is in popup mode. The problem is that when I view the popup, the initial default value for the EndDate picker is always set to the current date, even though when I look inside the page source, the "value" attribute for the DatePicker is set to my future date:

value=\"2013-12-06\"

Why is the default value displayed in the DatePicker always the current date regardless of the value I am passing to it?

2
I don't understand. You are setting the end date to two days after the current date. That should be the date you see, isn't it? - ataravati
Correct, I am setting the end date to two days after the current date, however when the popup appears, the current date is the selected value in the datepicker. It's as if it's ignoring the passed in value. - Pete
OK, you mean the End Date is the current date, instead of being two days after the current date? - ataravati

2 Answers

4
votes

Figured it out. It turns out that kendo will initialize the new underlying model on its own, and it will override any default value set in the datepicker. Since the date fields in my form were non-nullable, it initialized them by default to the current date. In order to tell kendo an alternate default value for newly created models, I had to modify the datasource property in the grid (which spawns the popup form).

.DataSource(dataSource => dataSource
                .Ajax()
                .Sort(sort => sort.Add(s => s.RunDate).Descending())
                .Model(model =>
                     {
                       model.Id(c => c.Id);
                       //These next 3 lines tell kendo the default values to use 
                       //when creating a new model. This fixed my problem.
                       model.Field(c => c.EndDate).DefaultValue(DateTime.Today.AddDays(7));
                       model.Field(c => c.StartDate).DefaultValue(DateTime.Today);
                       model.Field(c => c.RunDate).DefaultValue(DateTime.Today);
                     })
                 .Events(events => events.Error("error"))
                 .Create(create => create.Action("Create", "MyController"))
                 .Read(read => read.Action("Read", "MyController"))
                 .Destroy(update => update.Action("Destroy", "MyController"))
      )
0
votes

I would set the default in your controller/action method, similar to this:

public ActionResult()
{
    var model = new MyViewModel
    {
        EndDate = DateTime.Now.AddDays(2)
    }

    return View(model);
}

and then get rid of the .Value(DateTime.Now.AddDays(2)) on the DatePickerFor