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

[Solved] DataBound DateTimePicker loses ViewModel value on post

2 Answers 104 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.
Reid
Top achievements
Rank 2
Reid asked on 03 Feb 2012, 06:46 PM

 

Hi All,

I am using a DateTimePicker on a view that performs a search.  This is also happening on a NumericTextBox on the same view.  The controls are initialized after the ViewModel is initialized when the view is first shown.  At that point they are displaying the right values from the View Model.

The problem is when the user fills one or more fields in and submits the search, the DateTimePickers and NumericTextBox do not hold the values the user entered as it comes back from the search.  I can observe this in the debugger  in the Ajax method(the [Grid Action]) right before the data is assigned to the Grid and the View returns.  The adjacent typical MVC TextBox markup (below)

<%= Html.TextBoxFor(model => model.SearchCriteria.BatchNumber)%>

works in that the model on post has those values.  Any Telerik controls do not update the ViewModel they are bound to. 

A bit more context, there is also a Telerik Grid on the View that shows the Results of the search that is using Ajax, and working perfectly.  The first search works and the values are in the post for the dates but after the search returns (and in the debugger of the Ajax call that fires) the results are that the controls load the current date time. 

And when you adjust the values to a different date and re-submit the view, the values are again not what was entered or selected.  I am also not sure in the markup if you should be using (setting the value) the .Value property or not.  I ahve tried it both ways.

Here is the markup for one of the two DateTimePickers ..

<%= Html.Telerik().DatePickerFor(ps => ps.SearchCriteria.SearchStartDate)  
    .Name("start-date")
    .Value(Model.SearchCriteria.SearchStartDate)                                             
    .Min(( ( DateTimePickerAttributes ) Model.DateTimePickerSettings["start-date"] ).MinDate.Value)
    .Max(( ( DateTimePickerAttributes ) Model.DateTimePickerSettings["start-date"] ).MaxDate.Value)
    .ShowButton(Model.DateTimePickerModel.DateAttributes.ShowButton.Value)                                                              
    .OpenOnFocus(Model.DateTimePickerModel.DateAttributes.OpenOnFocus.Value) %>



I have tried not addressing the .Value property and the result is the same.  And again the NumericTextBox does not retain it's value as it comes back from the search (which is an ajax call) like the DateTimePickers.

Any help would be appreciated.

Thanks,

Reid

2 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 06 Feb 2012, 10:29 AM
Hello Reid,

 
After a quick review of the code snippet, I noticed that the name of the  DatePickerFor is set using Name() method. I believe this cause the issue, because the "ps.SearchCriteria.SearchStartDate" is different than "start-date" and thus the ModelBinder will not be able to update the Model. I will suggest you not use Name() method and check if this fix the problem. 

Regards,
Georgi Krustev
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Reid
Top achievements
Rank 2
answered on 06 Feb 2012, 03:13 PM
Hi Georgi,

Thanks for that reply!  When I removed the line that was setting the name property it works!

Best wishes,

Reid
Tags
Date/Time Pickers
Asked by
Reid
Top achievements
Rank 2
Answers by
Georgi Krustev
Telerik team
Reid
Top achievements
Rank 2
Share this question
or