grid TimeSpan [object Object] asp.net MVC

2 posts, 0 answers
  1. Azmi
    Azmi avatar
    1 posts
    Member since:
    Mar 2016

    Posted 18 Apr Link to this post

    Hey

    I have problem with the grid his not showing createdTime  Why ? I so Tired for searching solution  plzz help me Im beginner  In  kendo and that my code :

    View :
    @(Html.Kendo().Grid<appfox.Models.Address>()
            .Name("grid")
            .Columns(columns =>
            {
                columns.Bound(p => p.AddressLine1);
                columns.Bound(p => p.AddressLine2).Visible(false);
                columns.Bound(p => p.AddressPostalCode);
                columns.Bound(p => p.CreatedDate).Format("{0:dd-MM-yyyy}").EditorTemplateName("Date");
                columns.Bound(p => p.CreatedTime).Format("{0:HH:mm:ss}").EditorTemplateName("Date");
                columns.Command(command => command.Custom("ViewDetails").Click("showDetails")).Width(180);


            })
                       .Resizable(resize => resize.Columns(true))
                        .Reorderable(reorder => reorder.Columns(true))
            .HtmlAttributes(new { style = "height: 380px;" })
            .Scrollable()
            .Groupable()
                .Sortable()
                
            
            .Pageable(pageable => pageable
                .Refresh(true)
                .PageSizes(true)
                .ButtonCount(5))
            .DataSource(dataSource => dataSource
                .Ajax()
                       .Model(model =>
                        {
                            model.Field(x => x.AddressId).Editable(false);
                            model.Id(x => x.AddressId);


                        })
                .Read(read => read.Action("List", "Test"))
                
                )
     

    )

    @(Html.Kendo().Window().Name("Details")
        .Title("Customer Details")
        .Visible(false)
        .Modal(true)
        .Draggable(true)
            .LoadContentFrom("List", "Test")
        .Width(300))

    _____________________________________________________________________________________

    Class:

     public partial class Address
        {
            public int AddressId { get; set; }
            public string AddressLine1 { get; set; }
            public string AddressLine2 { get; set; }
            public Nullable<byte> AddressPostalCode { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string Country { get; set; }
            public string TimeZone { get; set; }
            public string CreatedBy { get; set; }
            public System.DateTime CreatedDate { get; set; }
            public System.TimeSpan CreatedTime { get; set; }
            public string UpdatedBy { get; set; }
            public System.DateTime UpdatedDate { get; set; }
            public System.TimeSpan UpdatedTime { get; set; }
        }
    }

     

    Controller 

            public ActionResult Index1([DataSourceRequest]DataSourceRequest request)
            {
                List<Address> omList = new List<Address>();
                Address om = new Address();


                using (Test1Entities objContext = new Test1Entities())
                {
                    var q = objContext.Address.ToList();
                    omList = getOrders(q);

                    DataSourceResult result = omList.ToDataSourceResult(request);
                    return View(result);

                    // return omList;
                }


            }

            private List<Address> getOrders(List<Address> olist)
            {
                List<Address> omList = new List<Address>();
                foreach (var ads in olist)
                {
                    Address o = new Address();
                   
                    o.AddressLine1 = ads.AddressLine1;
                    o.AddressLine2 = ads.AddressLine2;
                    o.AddressPostalCode = ads.AddressPostalCode;
                    o.City = ads.City;
                    o.State = ads.State;
                    o.Country = ads.Country;
                    o.TimeZone = ads.TimeZone;
                    o.CreatedBy = ads.CreatedBy;
                    o.CreatedDate = ads.CreatedDate;
                    omList.Add(o);
                }

                return omList;
            }

            public ActionResult List([DataSourceRequest]DataSourceRequest request)
            {
                List<Address> omList = new List<Address>();
                Address om = new Address();


                using (Test1Entities objContext = new Test1Entities())
                {
                    var q = objContext.Address.ToList();
                    omList = getOrders(q);

                    DataSourceResult result = omList.ToDataSourceResult(request);
                    return Json(result, JsonRequestBehavior.AllowGet);

                    // return omList;
                }
            }

  2. Dimiter Topalov
    Admin
    Dimiter Topalov avatar
    353 posts

    Posted 20 Apr Link to this post

    Hello Azmi,

    [Object object] is rendered because the corresponding column is of complex type (struct), and cannot be serialized and rendered properly.

    The easiest approach to solve the problem is simply to use the CreatedDate property, bind another column to it and format it to show the time only:

    ...
    columns.Bound(p => p.CreatedDate).Format("{0:hh:mm:ss}").Title("Created Time").EditorTemplateName("Date");
    ...

    Another option is to use the ClientTemplate() method.

    Let me know if you need further assistance.

    Regards,
    Dimiter Topalov
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Kendo UI is VS 2017 Ready
Back to Top