Strange behavior when using objects within objects using Grid.

4 posts, 0 answers
  1. Ken
    Ken avatar
    4 posts
    Member since:
    Sep 2012

    Posted 31 Mar 2013 Link to this post

    I am unable to get the grid to show the date properly when using objects within objects.
    Here's a small example of where I am seeing this problem

    HomeController.cs

    public ActionResult Index()
    {
        return View();
    }
     
    public static IEnumerable<Models.Customer> GetCustomers()
    {
        var db = new Data.SomeDataContext();
        var query = ...
        return query;
    }
     
    public static IEnumerable<Models.Orders> GetOrders()
    {
        var db = new Data.SomeDataContext();
        var query = ...
        return query;
    }
     
    public ActionResult CustomerOrder_Read([DataSourceRequest] DataSourceRequest request)
    {
        var query = from o in GetOrders()
                     join c in GetCustomers() on o.CustomerAccountNumber equals c.AccountNumber
                     select new CustomerOrder
                     {
                         Order = o,
                         Customer = c
                     };
     
        return Json(query.ToDataSourceResult(request));

    }

    ~/Home/Index.chtml

    <div>
        @(Html.Kendo().Grid<Apex.Models.CustomerOrder>()
            .Name("Grid")
            .Columns(col =>
            {
                col.Bound(p => p.Order.OrderId);
                col.Bound(p => p.Order.Date);
                col.Bound(p => p.Order.Status);
                col.Bound(p => p.Customer.Name);
                col.Bound(p => p.Customer.Mobile);
            })
            .DataSource(data => data
                .Ajax()
                .PageSize(10)
                .Read(read => read.Action("ServiceOrder_Read", "Home"))
            )
            .Sortable()
            .Pageable()
        )
    </div>

    ExampleModels.cs

    public class CustomerOrder
    {
        public Customer Customer { get; set; }
        public Order Order { get; set; }
    }
    public class Customer
    {
        public string AccountNumber { get; set; }
        public string Name { get; set; }
        public string Mobile { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime ModifiedDate { get; set; }
    }
     
    public class Order
    {
        public string Status { get; set; }
        public string OrderId { get; set; }
        public DateTime Date { get; set; }
    }

    Result

    The problem is that the grid results should show a DateTime; However, it shows this...
    Order    Date                     Status    Name    Mobile      
    00-01 /Date(1364670000000)/ Open Adam 111-111-1111
    00-02 /Date(1364670000000)/ Closed Eve 222-222-2222
    00-03 /Date(1364670000000)/ Open Buddha 333-333-3333

  2. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2194 posts

    Posted 02 Apr 2013 Link to this post

    Hi Ken,

     

    Basically in current case showing the complex field can be achieved by parsing the date manually (as the dates are parsed only on the first level of the model) in the column template:

    columns.Bound(p => p.Employee.BirthDate).ClientTemplate("#=Employee.BirthDate ? kendo.format('{0:d}', kendo.parseDate(Employee.BirthDate)) : ''#");
    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. Ken
    Ken avatar
    4 posts
    Member since:
    Sep 2012

    Posted 02 Apr 2013 Link to this post

    Vladimir,

    Your solution worked exactly as needed. I appreciate the clear solution provided.

    I don't see this behavior happening for other data types (ex. String, Int, Etc). Would you recommend that I still parse these as a precaution, or is this behavior only seen on DateTime?
  4. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2194 posts

    Posted 04 Apr 2013 Link to this post

    Hi Ken,

     
    Basically only the Date type requires parsing and it's safe to not parse the other data types.

    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!
Back to Top