Date/Time issue

6 posts, 0 answers
  1. Steven
    Steven avatar
    29 posts
    Member since:
    Jul 2012

    Posted 27 Sep 2012 Link to this post

    I have setup a grid to display a complex object like so:

    public class A
    {
        public DateTime Created { get; set; }
    }

    public class B
    {
        public A Subobject { get; set; }
        public DateTime Created { get; set; }
    }

    When I pass a list of B objects, the Created fields for both A and B are set properly.  However, while the Kendo grid appears to handle B's Created property as a Date/Time, A's Created property appears to get converted to a string as it appears in the format "/Date(#############)/" when displayed in the grid.

    This appears to cause A's Created field to be posted back as a Date/Time.Min value (01/01/0001) when the record is Updated, instead of the "real" value when it was received from the server during the Read operation.  This causes ModelState.IsValid to be false.

    Is there any way to fix this?  I found a post related to this at http://www.kendoui.com/forums/ui/grid/date-column-type-doesn-t-work-when-data-source-is-a-datatable.aspx, but can't seem to find where to set a ModelFieldDescriptor in MVC.

    EDIT:

    Some additional information.  This only happens when the Delete AJAX method is called.  When the Edit AJAX method is called, the Subobject property of object B is null and ModelState.IsValid is true.
  2. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2203 posts

    Posted 02 Oct 2012 Link to this post

    Hi Steven, 

    You can access the value of the Date field with B.Subobject in your template, however using complex models currently is not supported out-of-the-box and the B.Subobject will return string like "/Date(1344200400000)/".

    You should flatten your model or implement workaround like:

    • Creating client template where the value of B.Subobject is parsed by javascript Date function:
    #= kendo.toString(new Date(parseInt(B.Subobject.replace(/[A-Za-z$--/]/g, ""))),"dd MMMM yyyy") #

    • If you are using KendoUI Web Grid, you can parse the data received from the server, before the Grid loads it using the shema.parse method.
    script type="text/javascript">
        $(document).ready(function () {
            var dataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: "home/indexjson",
                        dataType: "json"
                    }
                },
                // determines if changes will be send to the server individually or as batch
                batch: true,
                schema: {
                    parse: function (response) {
                        for (var currentObject in response) {
                            //Parsing the data before its used
                            response[currentObject].Subobject = response[currentObject].Subobject.Date;
                        };
                        return response;
                    },
                    model: {
                        id: "OrderID",
                        fields: {
                            OrderID: { validation: { required: true} },
                            Subobject: { type: "date" },
                            OrderDescription: { validation: { required: true} }
                        }
                    }
                }
                //...
            });
      
            $("#grid").kendoGrid({
                dataSource: dataSource,
                groupable: true,
                scrollable: true,
                sortable: true,
                pageable: true,
                filterable: true,
                editable: true,
                columns: [
                          {
                              field: "OrderID",
                              title: "Order ID"
                          },
                          {
                              field: "Subobject",
                              title: "Date",
                              format: "{0:dd MM yyyy}"
      
                          },
                          {
                              field: "OrderDescription",
                              title: "Order Description"
                          }]
            });
        });
    </script>


    Kind Regards,
    Vladimir Iliev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. John
    John avatar
    7 posts
    Member since:
    Jul 2014

    Posted 04 Nov 2014 in reply to Vladimir Iliev Link to this post

    Will a fix for this eventually make it into Kendo UI MVC?
  4. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2203 posts

    Posted 07 Nov 2014 Link to this post

    Hi John,


    Please note that the described behavior is not an issue - the DataSource "schema" doesn't support parsing nested fields which is the reason why the dates are not parsed. As I mention possible solution is to either flatter your model using ViewModel or manually parse the nested fields on the client side. The latest can be achieved using the "Custom" DataSource builder which allows the developer to specify "schema.parse" function:

    .DataSource(d => d
        .Custom()
        .Schema(schema => {
            schema.Parse(@<text>
                    function(data) {
                                   //parse the nested fields here
                        return data;
                    }
            </text>);

    Regards,
    Vladimir Iliev
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  5. Saad
    Saad avatar
    1 posts
    Member since:
    Aug 2015

    Posted 18 Oct 2017 in reply to Vladimir Iliev Link to this post

    I have the same issue, please fix it
  6. Stefan
    Admin
    Stefan avatar
    2918 posts

    Posted 23 Oct 2017 Link to this post

    Hello, Saad,

    The discussed behavior is expected based on the supported functionalities of the dataSource.

    If the desired result is to use a schema with complex objects, please make a feature request in our feedback portal and based on its popularity we may implement it in a future release:

    http://kendoui-feedback.telerik.com/forums/127393-kendo-ui-feedback/category/63716-data-source

    Regards,
    Stefan
    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.
Back to Top