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

Bug in ToDataSourceResult / FirstSortableProperty method

3 Answers 133 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
ustr
Top achievements
Rank 1
ustr asked on 27 Jun 2016, 10:50 AM

Hello,

there is bug in ToDataSourceResult method, namely internal FirstSortableProperty method. The FirstSortableProperty method is called when passed DataSourceRequest has no Sorts list. It uses reflection to select first property of queryable entity.

If your entity class has a "calculated" readonly property that is not mapped to the database entity, the FirstSortableProperty method might select it so the LINQ query fails. It supposed to select public instance properties with getters and setters only.

Thanks.

3 Answers, 1 is accepted

Sort by
0
Vasil
Telerik team
answered on 29 Jun 2016, 01:22 PM
Hi,

The GetProperties is called without BindingFlags parameter, so it returns Public properties. So it already do what you suggest it should be doing.

Could you give us an example where you find that the method fails?

Regards,
Vasil
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
0
ustr
Top achievements
Rank 1
answered on 07 Jul 2016, 09:27 PM
It supposed to select public instance properties with getters and setters only. If you have a readonly public property (property that have a getter but not a setter) such method should not be taken into the account.
0
Vasil
Telerik team
answered on 08 Jul 2016, 11:02 AM
Hi,

There is still not problem to sort a collection of objects by property that does not have public setter.

@(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.CustomerViewModel>()
        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(c => c.custom);
            columns.Bound(c => c.ContactName;
            columns.Bound(c => c.ContactTitle);
            columns.Bound(c => c.CompanyName);
            columns.Bound(c => c.Country);
        })
        .Scrollable()
        .Groupable()
        .Sortable()

Model that contain filed with getter only:

public class CustomerViewModel
{
    public string custom { get { return CompanyName + ContactName; } }
    public string CustomerID { get; set; }
    public string CompanyName { get; set; }
    public string ContactName { get; set; }
    //....
}

using ToDataSourceResult

public IActionResult Customers_Read([DataSourceRequest] DataSourceRequest request)
{
    return Json(GetCustomers().ToDataSourceResult(request));
}

Passing the values:

private static IEnumerable<CustomerViewModel> GetCustomers()
{
    var northwind = new SampleEntitiesDataContext();
    return northwind.Customers.ToList().Select(customer => new CustomerViewModel
    {
        CustomerID = customer.CustomerID,
        CompanyName = customer.CompanyName,
        ContactName = customer.ContactName,
        //..... no need to set "custom" here, since it does not have setter
    });
}

If you still have troubles, please give us more information what is different in your case. Or an example where the ToDataSourceResult fails, cause by sorting by read-only property.

Regards,
Vasil
Telerik by Progress
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
General Discussions
Asked by
ustr
Top achievements
Rank 1
Answers by
Vasil
Telerik team
ustr
Top achievements
Rank 1
Share this question
or