Change FilterDescriptor.Member name in the view

3 posts, 1 answers
  1. Jacob
    Jacob avatar
    45 posts
    Member since:
    Sep 2014

    Posted 23 Jan 2015 Link to this post

    Hi,

    How to change the FilterDescriptor.member name in the view ?

    columns.Bound(e => e.Name).Filterable(true);

    In the above sample, the filter will set the Member to "Name", because this is the name of the property bound to the column.
    What if I want to change the member name, is that possible ??
    Thanks.
  2. Answer
    Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 27 Jan 2015 Link to this post

    Hello,

    You can change that member in your action method by setting the corresponding property of the DataSourceRequlest.Filters object. Here is some sample code:

    public ActionResult Read([DataSourceRequest] DataSourceRequest request)
    {
            foreach (var descriptor in request.Filters)
            {
                if (descriptor is CompositeFilterDescriptor)
                {
                    foreach (FilterDescriptor childDescriptor in ((CompositeFilterDescriptor)descriptor).FilterDescriptors)
                    {
                        if (childDescriptor.Member == "Name")
                        {
                            childDescriptor.Member = "LastName";
                        }
                    }
                }
                else
                {
                    var filterDescriptor = descriptor as FilterDescriptor;
                    if (filterDescriptor.Member == "Name")
                    {
                        filterDescriptor.Member = "LastName";
                    }
                }
     
                
            }
            return data.ToDataSourceResult(request);
    }





    Regards,
    Atanas Korchev
    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. Jacob
    Jacob avatar
    45 posts
    Member since:
    Sep 2014

    Posted 27 Jan 2015 in reply to Atanas Korchev Link to this post

    Hi Atanas,
    Much appreciated.
    Actually I have already done something similar, but in a more generic way by using Attributes:

    public static void ConvertFilterToMatchDestination<T>(this FilterDescriptor _this)
    {
        PropertyInfo prop = typeof(T).GetProperty(_this.Member);
        if (prop == null)
            return;
        Attribute att = prop.GetCustomAttribute(typeof(KendoFilterAttribute));
        if (att != null && att is KendoFilterAttribute)
        {
            KendoFilterAttribute attribute = att as KendoFilterAttribute;
            if (!string.IsNullOrEmpty(attribute.BaseFieldName))
                _this.Member = attribute.BaseFieldName;
            if (attribute.ConvertDecimalToInt)
            {
                decimal d = 0;
                if (decimal.TryParse(_this.Value.ToString(), out d))
                    _this.Value = decimal.ToInt32(d);
            }
        }
    }

    There are two problems when dealing with filtering in MVC: 1 The domain model and the view model doesn't necessary share property names (the reason for this question), 2 there is no such things as an int filter in Kendo.Grid, only a decimal filter.
    The last thing cause problems when building dynamic expressions (.Net Expression) because int database fields is not comparable with decimals values.

    Anyway, I will accept your comment as the answer, because the code does what I asked for :-)
    Thanks.
Back to Top