SORT - ViewModel with Property which is from another Entity

5 posts, 1 answers
  1. Vladimír
    Vladimír avatar
    3 posts
    Member since:
    Aug 2011

    Posted 24 Jul 2013 Link to this post

    Hi Telerik,

    it is possible sort data in grid on server side with viewmodel which has navigation property to customer. Or I have to create new SQL view which will group data from two tables(Order and Customer together). I will be able to sort data from mssql view.

    // Models
    public class Order
      public int OrderID { get; set; }
      public Customer Customer { get; set; }

    public class Customer
      public string ContactName { get; set; }

    // Action
    public ActionResult Read([DataSourceRequest] DataSourceRequest request)
      var dataContext= new DataContext();
      var orders = dataContext.Orders;
      var result = orders.ToDataSourceResult(request, o => new {OrderID = o.OrderID, CustomerName = o.Customer.ContactName});
      return result;

    Now i want sort data according to CustomerName then error occures.
    MSSQL view solve this problem? 
  2. Answer
    Petur Subev
    Petur Subev avatar
    1882 posts

    Posted 26 Jul 2013 Link to this post

    Hello Vladimír,

    Actually you do not need to flatten your model and you can bind the column directly to that nested field. And it should work just fine.

    Here is how I modified the following demo on my side.

        .Columns(columns =>
            columns.Bound(p => p.ProductName);
            columns.Bound(p => p.Category).ClientTemplate("#=Category.CategoryName#").Width(160);
            columns.Bound(p => p.Category.CategoryName).Width(160);
            columns.Bound(p => p.UnitPrice).Width(120);
            columns.Command(command => command.Destroy()).Width(90);
        .ToolBar(toolBar =>
        .Editable(editable => editable.Mode(GridEditMode.InCell))

    And sorting behaves exactly as expected.

    Kind Regards,
    Petur Subev
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET AJAX banner
  4. Silviya
    Silviya avatar
    5 posts
    Member since:
    Mar 2015

    Posted 28 May 2015 Link to this post

    Reviving an old topic but I was having the same issue with sorting and when I read the answer I was shocked. Thought that wasn't possible because I have tried that million times. My grid keeps accepting only flat models. In the example provided, that means an error like:

    Uncaught TypeError: Cannot read property CategoryName' of undefined

    Do  I need anything special to be able to achieve binding to related table? 


  5. Silviya
    Silviya avatar
    5 posts
    Member since:
    Mar 2015

    Posted 28 May 2015 Link to this post

    After being certain it could work, I invested more time in trying to figure it out. So came up with this solution which I don't find very pretty. Please correct me if I could do it better.

    Using the above example logic again:



      DataSourceResult result = products.ToDataSourceResult(request, product=> new {
                    ID = product.ID,
                    Category= new Category
                        CategoryName= Product.Category.CategoryName


    So unless Category is defined as an instance of the Category object, I cannot access any of its properties since it is returned as undefined.


    Is that how it is supposed to be or there is a more elegant way of achieving the goal?



  6. Boyan Dimitrov
    Boyan Dimitrov avatar
    1728 posts

    Posted 01 Jun 2015 Link to this post

    Hello Silviya,

    Indeed a instance of the Category class should be defined in order to access and set its properties.

    Boyan Dimitrov
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for ASP.NET AJAX banner