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

I just want to post a datetimepicker's value to the server.......

4 Answers 55 Views
Date/Time Pickers
This is a migrated thread and some comments may be shown as answers.
Matt Miller
Top achievements
Rank 1
Matt Miller asked on 09 Sep 2014, 04:01 PM
i have a datetime picker.....

<input type="datetime" id="dateOfService" name="dateOfService" style="width:250px;" />

in $(document).ready(function (){........

var dateOfService = $("#dateOfService").kendoDateTimePicker({
           value: new Date(),
           culture: "en-US",
           format: "dd/MM/yyyy HH:mm:ss",
           parseFormats: ["dd/MM/yyyy HH:mm:ss"]
       });

User clicks a save button, post values back to server:

$.post("/Home/CreateJob",
                 {
                     dateOfService: new Date($("#dateOfService").data("kendoDateTimePicker").value())
                 }).success(function (data) {
                     console.log("success");                    
                 }).fail(function (data) {                    
                     console.log("fail") ;
                 });


In my controller........

[HttpPost]    
       public JsonResult CreateJob(string dateOfService)
       {
           try
           {
               DateTime d = Convert.ToDateTime(dateOfService); //can't do this. not recognized as valid date/time
           }
           catch(Exception e){
 
            }
 
          


The string is in the format "Tue Sep 09 2014 12:00:03 GMT-0400 (Eastern Standard Time)".


What am i missing here? Why is this difficult at all? And how to i resolve the problem?

4 Answers, 1 is accepted

Sort by
0
Holger
Top achievements
Rank 1
answered on 11 Sep 2014, 06:58 AM
Hello Matt,

Try to use DateTime as argument type in the controller action. The model binder should be able to correctly convert the DateTime from json.

Regards,
Holger
0
Matt Miller
Top achievements
Rank 1
answered on 11 Sep 2014, 12:54 PM
Thanks for the reply, but i've tried that as well. When i make the argument type Datetime in the controller , the action method isn't called at all???? Like you , i first assumed the type should be DateTime...

I wish someone from telerik would respond to this and explain what my newbie problem is......
0
Holger
Top achievements
Rank 1
answered on 12 Sep 2014, 08:35 AM
Hello Matt,

this works for me:

Client side code:
var data = JSON.stringify(new Date($("#dateOfService").data("kendoDateTimePicker").value()));
 
$.ajax({
    url: '/Home/CreateJob',
    type: 'POST',
    contentType: 'application/json',
    dataType: 'json',
    data: data,
    error: function () {
        console.log('fail');
    },
    success: function () {
        console.log('success');
    }
});

Controller action:
public void CreateJob([FromBody]DateTime dateOfService)
{
.
.
.
}

Regards,
Holger
0
Georgi Krustev
Telerik team
answered on 12 Sep 2014, 09:16 AM
Hello Matt,

It will be best if you can send us a repro test project? Thus we will be able to observe the implementation and advice you further.

Regards,
Georgi Krustev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Date/Time Pickers
Asked by
Matt Miller
Top achievements
Rank 1
Answers by
Holger
Top achievements
Rank 1
Matt Miller
Top achievements
Rank 1
Georgi Krustev
Telerik team
Share this question
or