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

SORT - ViewModel with Property which is from another Entity

4 Answers 273 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Vladimír
Top achievements
Rank 1
Vladimír asked on 24 Jul 2013, 07:53 AM
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? 

4 Answers, 1 is accepted

Sort by
0
Accepted
Petur Subev
Telerik team
answered on 26 Jul 2013, 07:00 AM
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.

@(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ClientProductViewModel>()
    .Name("grid")
    .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 =>
        {
            toolBar.Create();
            toolBar.Save();
        })
    .Editable(editable => editable.Mode(GridEditMode.InCell))
    .Pageable()
    .Sortable()

And sorting behaves exactly as expected.

Kind Regards,
Petur Subev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Silviya
Top achievements
Rank 1
answered on 28 May 2015, 07:01 AM

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? 

 

0
Silviya
Top achievements
Rank 1
answered on 28 May 2015, 09:41 AM

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:

Action:

...

  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?

 

Thanks!

0
Boyan Dimitrov
Telerik team
answered on 01 Jun 2015, 08:32 AM

Hello Silviya,

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

Regards,
Boyan Dimitrov
Telerik
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
Tags
Grid
Asked by
Vladimír
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Silviya
Top achievements
Rank 1
Boyan Dimitrov
Telerik team
Share this question
or