Bug in ToDataSourceResult / FirstSortableProperty method

4 posts, 0 answers
  1. ustr
    ustr avatar
    4 posts
    Member since:
    May 2011

    Posted 27 Jun Link to this post

    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.
  2. Vasil
    Admin
    Vasil avatar
    1547 posts

    Posted 29 Jun Link to this post

    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
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. ustr
    ustr avatar
    4 posts
    Member since:
    May 2011

    Posted 07 Jul in reply to Vasil Link to this post

    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.
  5. Vasil
    Admin
    Vasil avatar
    1547 posts

    Posted 08 Jul Link to this post

    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
Back to Top
UI for ASP.NET MVC is VS 2017 Ready