DateTime not posting correctly to controller

5 posts, 1 answers
  1. Jorge Torres
    Jorge Torres avatar
    3 posts
    Member since:
    Jun 2010

    Posted 05 Jul 2018 Link to this post

    Hello, 

    I have an issue regarding the posting of a DateTimePicker value to a controller. Code is as follows:

    View:

    1.@(Html.Kendo().DateTimePickerFor(model => model.FechaLimite)
    2.      .Format("dd/MM/yyyy hh:mm tt")                                        
    3.      .TimeFormat("hh:mm tt")
    4.      .Value(DateTime.Now)
    5.      .HtmlAttributes(new { @class = "form-control" }))

    Controller:

    1.[HttpPost]
    2.[ValidateAntiForgeryToken]
    3.public ActionResult Create([Bind(Include = "FechaLimite")] TiqueteViewModel viewModel)
    4.{
    5.    if (ModelState.IsValid)
    6.    {
    7.    }
    8.}

    There are other fields inside the viewModel which I have omitted for the sake of simplicity, but it's a really weird behavior, I'll explain:

    1. If I POST the form with the default value that's loaded into the DateTimePicker (DateTime.Now), it posts correctly, see screenshot "POSTDefaultValue".
    2. If I POST the form changing either the date or the time, it POSTS nothing, see screenshot "POSTChangeValue".
    3. If I remove the "Format" from the DateTimePicker, it always posts correctly, however, I need the user to see it in am/pm format, see screenshot "POSTWithoutFormat".

    When the value is not POSTed, I get the error in screenshot "DateTimeError", like in case number 2.

    So I'm guessing it has something to do with the format validation once the date is changed. I have set the same culture to both kendo on the client-side and on the server side using the guides you have available. Just in case, the culture I've set is "es-CR".

    Any idea what could be causing this behavior ? If an example is needed I can isolate the issue and attach a project.

     

  2. Viktor Tachev
    Admin
    Viktor Tachev avatar
    2384 posts

    Posted 09 Jul 2018 Link to this post

    Hello Jorge,

    Please make sure that the same culture is used on both the client and server as described in the following article. 


    Also, when using custom formats for the dates you may need to override the default validation format used client-side. Check out the article below that illustrates how this can be done.


    Give the approach and let me know how the behavior changes. 


    Regards,
    Viktor Tachev
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Jorge Torres
    Jorge Torres avatar
    3 posts
    Member since:
    Jun 2010

    Posted 11 Jul 2018 in reply to Viktor Tachev Link to this post

    Hi Viktor, 

    Thanks for the response and the assistance, however I've tried everything you've suggested and I still can't get the value to POST correctly when I change the value of the DateTimePicker to anything else than the loaded value of DateTime.Now.

    Could you please take a look at the attached example ? The behavior that I describe in the first post is the same in the example.

    Thanks in advance.

  4. Answer
    Viktor Tachev
    Admin
    Viktor Tachev avatar
    2384 posts

    Posted 13 Jul 2018 Link to this post

    Hi Jorge,

    Thank you for the sample project.

    After investigating the behavior I noticed that when the Date was returned from the server it included a space in the AM/PM indicator (e.g. a. m. ; p. m.). However, the format used for AM/PM does not include this space. Thus, when the date is changed it does not include a space and the value submitted to the server is not considered valid.

    I have modified the cultures file to include this time format and after this the project was working as expected on my end. You will find the updated sample attached. Give it a try and let me know how it works for you.

    As a workaround you can use the modified culture file in your project. I have updated the relevant culture file and the modification will be present in the next internal build and also in the official release. 

    Regards,
    Viktor Tachev
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  5. Jorge Torres
    Jorge Torres avatar
    3 posts
    Member since:
    Jun 2010

    Posted 13 Jul 2018 in reply to Viktor Tachev Link to this post

    Hi Viktor, 

    Thanks a lot for your assistance, it worked like a charm. 

    Best regards.

Back to Top