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

Date/Time issue

5 Answers 436 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steven
Top achievements
Rank 1
Steven asked on 27 Sep 2012, 06:00 PM
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.

5 Answers, 1 is accepted

Sort by
0
Vladimir Iliev
Telerik team
answered on 02 Oct 2012, 04:06 PM
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!
0
John
Top achievements
Rank 2
answered on 04 Nov 2014, 11:37 PM
Will a fix for this eventually make it into Kendo UI MVC?
0
Vladimir Iliev
Telerik team
answered on 07 Nov 2014, 10:23 AM
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.

 
0
Saad
Top achievements
Rank 1
answered on 18 Oct 2017, 09:35 AM
I have the same issue, please fix it
0
Stefan
Telerik team
answered on 23 Oct 2017, 07:03 AM
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.
Tags
Grid
Asked by
Steven
Top achievements
Rank 1
Answers by
Vladimir Iliev
Telerik team
John
Top achievements
Rank 2
Saad
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or