Setting a date in a DataItem from a DataSource using the "set" method

3 posts, 0 answers
  1. Toby
    Toby avatar
    20 posts
    Member since:
    Nov 2010

    Posted 09 Jun Link to this post

    I'm setting the value of a property using the following JavaScript code

    var documentList = $("#documentsList").data("kendoListView");
    var dataSource = documentList.dataSource;
    var dataUid ="data-uid");
    var item = dataSource.getByUid(dataUid);
    var datepicker = $("#documentDate").data("kendoDatePicker");
    item.set("DocumentDate", datepicker.value());
    if (dataSource.hasChanges()) {

    My controller is receiving the post when the datasource is synced and the other properties are being set. This is the C# of my controller

    public JsonResult Update([DataSourceRequest]DataSourceRequest request, InboxDocument document)
       return Json(new[] {SharePointHelper.UpdateInboxDocument(HttpContext, document)}.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);

    All the other properties that are set are being sent through, but when I look at the request sent through in F12 tools, the following is displayed.

    {DocumentDate: {errors: ["The value '6/21/2016 1:00:00 AM' is not valid for DocumentDate."]},…}

    This is the same for any other date values.



  2. Stephen
    Stephen avatar
    69 posts
    Member since:
    Jan 2011

    Posted 10 Jun in reply to Toby Link to this post

    If I remember correctly from the multiple time I've had to fight with dates....

    I think the problem is that datepicker.value() returns a string, not a date object.

    Then when you do the .set(), it is simply treated as a string and the model value is now set to a string...not a date anymore and so it does not get serialized as a date when sending to the server and the MVC model binder is unable to bind the string value to the DateTime field, probably due to not being able to parse that particular string format to a date.

    Try wrapping the datepicker.value() in a kendo.parseDate(), i.e.

    item.set("DocumentDate", kendo.parseDate(datepicker.value()));

    and see if that helps as this will set the the model value to an actual date object instead of "overwriting" the datatype to a string. 



  3. UI for ASP.NET AJAX banner
  4. Daniel
    Daniel avatar
    2110 posts

    Posted 13 Jun Link to this post


    The value method should always return a Date object or null and parseDate will automatically be called by the model if the field is declared as date which should also happen automatically when using the wrappers. The most likely reason for the problem is that the client-side and server side cultures are not the same in which case the dataSource will not serialize the dates as expected on the server.

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top