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

Change FilterDescriptor.Member name in the view

2 Answers 848 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jacob
Top achievements
Rank 1
Jacob asked on 23 Jan 2015, 05:31 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Atanas Korchev
Telerik team
answered on 27 Jan 2015, 12:23 PM
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.

 
0
Jacob
Top achievements
Rank 1
answered on 27 Jan 2015, 02:05 PM
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.
Tags
Grid
Asked by
Jacob
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Jacob
Top achievements
Rank 1
Share this question
or