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

Strange behavior when using objects within objects using Grid.

3 Answers 72 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ken
Top achievements
Rank 1
Ken asked on 31 Mar 2013, 09:22 PM
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

3 Answers, 1 is accepted

Sort by
0
Vladimir Iliev
Telerik team
answered on 02 Apr 2013, 02:26 PM
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!
0
Ken
Top achievements
Rank 1
answered on 02 Apr 2013, 03:05 PM
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?
0
Vladimir Iliev
Telerik team
answered on 04 Apr 2013, 11:59 AM
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!
Tags
Grid
Asked by
Ken
Top achievements
Rank 1
Answers by
Vladimir Iliev
Telerik team
Ken
Top achievements
Rank 1
Share this question
or