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

5 posts, 0 answers
  1. Matt Miller
    Matt Miller avatar
    23 posts
    Member since:
    Jan 2012

    Posted 09 Sep 2014 Link to this post

    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?
  2. Holger
    Holger avatar
    77 posts
    Member since:
    Nov 2012

    Posted 11 Sep 2014 in reply to Matt Miller Link to this post

    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
  3. Kendo UI is VS 2017 Ready
  4. Matt Miller
    Matt Miller avatar
    23 posts
    Member since:
    Jan 2012

    Posted 11 Sep 2014 in reply to Holger Link to this post

    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......
  5. Holger
    Holger avatar
    77 posts
    Member since:
    Nov 2012

    Posted 12 Sep 2014 in reply to Matt Miller Link to this post

    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
  6. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3707 posts

    Posted 12 Sep 2014 Link to this post

    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!
     
Back to Top
Kendo UI is VS 2017 Ready