This is a migrated thread and some comments may be shown as answers.

[Solved] DatePickerFor does not use value from model

1 Answer 128 Views
Date/Time Pickers
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Dave
Top achievements
Rank 1
Dave asked on 02 Jul 2012, 03:44 PM
Originally posted in the Calendar forum:

I am passing in dates to action method in the route like this:
http://mydomain/{controller}/{action}/2012-06-19

The action method receives this value in a "startDate" parameter on the action method and it is converted into a DateTime. So far so good.

Next I construct a ViewModel that contains a StartDate property of type DateTime. The date value passed into the action method is set as the value of the ViewModel's StartDate property. This ViewModel is passed to the View.

In my view I want to display a DatePicker using this date so I have the following:

@(Html.Telerik().DatePickerFor(x => x.StartDate)

.Min(Model.VitalSign.MinimumDate))


I would expect that the DatePicker's value would be taken from the value in the ViewModel. However, the DatePicker control shows the date as "2012-06-19" instead, presumably taking the value from the RouteValues collection instead.

How do I get the DatePickerFor method to use the value from my ViewModel? I tried .DatePicker().Name("startDate").Value(Model.StartDate) but it still shows "2012-06-19" in the DatePicker control.

Regards,
Dave

1 Answer, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 05 Jul 2012, 10:10 AM
Hi Dave,

Generally speaking the DatePicker tries to get the value from the ModelState. In your case you will need to either use different names for the datepicker/modelproperty or you could clear the ModelState manually with the following code invoked before rendering the DatePicker.
@{  ModelState["thename"]=null;
    // or
    ModelState.Clear(); }

I hope this helps.

Regards,
Petur Subev
the Telerik team
Check out the successor of Telerik MVC Extensions - Kendo UI for ASP.NET MVC - and deem it for new ASP.NET MVC development.
Tags
Date/Time Pickers
Asked by
Dave
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Share this question
or