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

[Solved] Datepicker value not returned to model

5 Answers 637 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.
Achilles
Top achievements
Rank 1
Achilles asked on 02 Dec 2011, 03:11 PM
Hi,

I am using an MVC Datepicker in an Edit template in the following way    

    <script type="text/javascript">
        //DatePicker client-side events.
        function onChangeDatePicker(e) {
            document.forms[0].submit();
        }
        </script>
@( Html.Telerik().DatePickerFor(m => m.StartDate)
            .Name("DatePicker")
            .ClientEvents(events => events
                     .OnChange("onChangeDatePicker")
                     )
    )


I can initialize the datepicker with via the view model. But when the view Posts back to the controller, the date value is not retained. It is initializes to "01/01/0001". What all other attributes do I have to set to bind the datetime value to the model ?

I am attaching a scaled down project where I use a Telerik datetime picker & a regular MVC datetime picker. On hitting the update button, the regular datetime picker sets the correct value to the model, But the telerik datetime value is not bound.

Thanks

Achilles

5 Answers, 1 is accepted

Sort by
0
Accepted
Georgi Krustev
Telerik team
answered on 02 Dec 2011, 05:52 PM
Hello Achilles,

 
When DatePickerFor extension is used you should not set the Name again. I will suggest you remove the Name("DatePicker").

Best regards,
Georgi Krustev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Achilles
Top achievements
Rank 1
answered on 09 Dec 2011, 09:21 AM
Thanks it helped. I am trying to implement a date spinner so that I can increment/decrement the date with a single click using external client APIs. A couple of questions ...

1. I am not able to retrieve the datepicker client-side object as

function getDatePicker(){
    //"DatePicker" is the value specified by the Name() method.
    var datePicker = $("#StartDate").data("tStartDate");
    return datePicker;
}


as, that is not the name specified by the Name() method. ( I am using DatePickerFor view model). I can retrieve an object by

var datePicker = $("#StartDate") but the data member of the object is undefined. I'm not sure what I should do next to get the value.    

2. Is there a way to increment the day of the date object ? like ...
function incrementDay() {
    // var dateval = document.getElementById("StartDate").value;
    var value = getDatePicker().value();
    value = value.AddDays(1);
    getDatePicker().value(value);
}


Thanks

Achilles
0
Georgi Krustev
Telerik team
answered on 09 Dec 2011, 03:21 PM
Hello Achilles,

 
I will suggest you check this help topic, which shows how to get the client object.

Regards,
Georgi Krustev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Achilles
Top achievements
Rank 1
answered on 09 Dec 2011, 03:35 PM
Hi Georgi,

Thanks for your quick reply. The help topic which you mentioned is the one which I used to formulate this code. The difference is that in the sample the 'DatePicker' has a name. In my case it is a 'DatePickerFor', from which I had removed the "Name" field as per your earlier suggestion, to bind to the ViewModel (which solved the initial problem)

Regards

Achilles
0
jonathan
Top achievements
Rank 1
answered on 31 May 2012, 10:21 PM
This was very helpful.  If you cannot assign a name to the DatePickerFor, how do you get it or use the same DatePickerFor control on multiple pages?
Tags
Date/Time Pickers
Asked by
Achilles
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Achilles
Top achievements
Rank 1
jonathan
Top achievements
Rank 1
Share this question
or