First attempt at a Grid; Fail

6 posts, 2 answers
  1. Kenb
    Kenb avatar
    15 posts
    Member since:
    Nov 2014

    Posted 27 Nov 2014 Link to this post

    Model

    public class Switch
    {
        [DisplayName("Switch Id")]
        public string SwitchId { get; set; }
    }

    Controller
    public class UserConsoleViewModelsController : Controller
    {
        public ActionResult FilteredSwitches_Read([DataSourceRequest]DataSourceRequest request)
        {
            var filteredSwitches = GetFilteredSwitches().ToDataSourceResult(request);  //3 records are here at runtime
            return Json(filteredSwitches);
        }
     
        private IEnumerable<Switch> GetFilteredSwitches()
        {
            return db.Switches.ToList();
        }
    }

    View

    @(Html.Kendo().Grid<Switch>()
        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(c => c.SwitchId).Width(140);
        })
        .HtmlAttributes(new { style = "height: 380px;" })
        .Scrollable()
        .Groupable()
        .Sortable()
        .Pageable(pageable => pageable
            .Refresh(true)
            .PageSizes(true)
            .ButtonCount(5))
        .DataSource(dataSource => dataSource
            .Ajax()
            .Read(read => read.Action("FilteredSwitches_Read", "UserConsoleViewModels"))
        )


    I get a grid, but there are no rows in it for the 3 records known to be in 'FilteredSwitches_Read()'.

    What am I doing wrong?

    Dave
  2. Answer
    Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2310 posts

    Posted 28 Nov 2014 Link to this post

    Hello Dave,


    The sample code looks correct. I would suggest you to check the developer tools console for JavaScript errors and also the network tab to assure that the request to the FilteredSwitches_Read action was successful.

    I am looking forward to hearing from you.

    Regards,
    Dimiter Madjarov
    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.

     
  3. Kenb
    Kenb avatar
    15 posts
    Member since:
    Nov 2014

    Posted 28 Nov 2014 in reply to Dimiter Madjarov Link to this post

    There was an error indicated in the dev tools...  apparently there was something about my model that the Json-ifier didn't like. Thinking I was using the control incorrectly, I presented a simplified version of my model in my first post.  I am now using that simplified model and it is working.  Now to slowly work toward the more complex model until I find the issue.

    Thank you!
  4. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2310 posts

    Posted 28 Nov 2014 Link to this post

    Hello Dave,


    Probably the reason was a circular reference. If that is the case you should extract a ViewModel containing only the needed properties. More information is available in the documentation.

    Regards,
    Dimiter Madjarov
    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. Kenb
    Kenb avatar
    15 posts
    Member since:
    Nov 2014

    Posted 28 Nov 2014 in reply to Dimiter Madjarov Link to this post

    Yes, a Circular Reference was indeed the error and indeed I did solve it with a less complex view model.  Now I have another issue. Please let me know if I should post this as a new question.

    My ViewModel looks like this.

    public class UserConsoleSwitchGridModel
    {
        public string SwitchId { get; set; }
        public string SentinelId { get; set; }
        public DateTime MostRecentNormal
        {
            get
            {
                var db = new ApplicationDbContext();
                try
                {
                    var throws = db.SwitchThrows
                        .Where(s => s.SwitchId == SwitchId)
                        .Where(a => a.Action == "Normal")
                        .OrderByDescending(t => t.Timestamp)
                        .Take(1);
     
                    var _throw = throws.ToList().ElementAt(0);               
                    return _throw.Timestamp;
     
                }
                catch
                {
                    return DateTime.MinValue;
                }
            }
        }
        [DisplayName("Most Recent Reverse Throw")]
        public DateTime MostRecentReverse
        {
            get
            {
                {
                    var db = new ApplicationDbContext();
                    try
                    {
                        var throws = db.SwitchThrows
                            .Where(s => s.SwitchId == SwitchId)
                            .Where(a => a.Action == "Reverse")
                            .OrderByDescending(t => t.Timestamp)
                            .Take(1);
     
                        var _throw = throws.ToList().ElementAt(0);                   
                        return _throw.Timestamp;
                    }
                    catch
                    {
                        return DateTime.MinValue;
                    }
                }
            }
        }
    }


    Controller

    public ActionResult FilteredSwitches_Read([DataSourceRequest]DataSourceRequest request)
    {
        var filteredSwitches = GetFilteredSwitches().ToDataSourceResult(request);
        return Json(filteredSwitches);
    }
     
    private IEnumerable<UserConsoleGridModel> GetFilteredSwitches()
    {
        var userid = User.Identity.GetUserId();
        var user = db.Users.First(u => u.Id == userid);
        return
            db.Switches.Include(i => i.Sentinel).Include(n => n.MostRecentNormalSwitchThrow).Include(r => r.MostRecentReverseSwitchThrow)
                .Where(c => c.CompanyId == user.Company.CompanyId)
                .Select(x => new UserConsoleGridModel()
                {
                    SwitchId = x.SwitchId,
                    SentinelId = x.SentinelId
                });
    }



    @(Html.Kendo().Grid<UserConsoleGridModel>()
        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(c => c.SwitchId).Width(140);
            columns.Bound(c => c.SentinelId);
            columns.Bound(c => c.MostRecentNormal);
            columns.Bound(c => c.MostRecentReverse);
        })
        .HtmlAttributes(new { style = "height: 380px;" })
        .Scrollable()
        .Groupable()
        .Sortable()
        .Pageable(pageable => pageable
            .Refresh(true)
            .PageSizes(true)
            .ButtonCount(5))
        .DataSource(dataSource => dataSource
            .Ajax()
            .Read(read => read.Action("FilteredSwitches_Read", "UserConsoleViewModels"))
        )
    )


    This works fine on first render, on refresh, of if I sort on either of the two string 'Id' columns, but if I sort on either of the DateTime columns I get this error:

    The specified type member 'MostRecentReverse' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

    How do you suggest I do this?  Please know that the value of either/both the two DateTime fields could change between sorts.

    Thanks!
  6. Answer
    Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2310 posts

    Posted 01 Dec 2014 Link to this post

    Hello Dave,


    The current problem does not seem directly related to Kendo UI, but to the way the data is retrieved. I found similar forum threads (for example here and here), that could help in resolving it.

    Regards,
    Dimiter Madjarov
    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.

     
Back to Top