FilterCondition.Contains for String in ThemedDataGridFor

Feb 7, 2012 at 3:06 PM



Is possible to use "FilterCondition.Contains" to filter string e String fields in a ThemedDataGridFor?

I edited the grid template to:

@DataFilterClauseHelpers.FilterClauseSelect(h1, h1.ViewData.Model.Condition, column.Field, null,                                    CanSortAttribute.AllowedForProperty(column.Field) |  FilterCondition.Contains)

But, if I use the condition "Contains", the filter does not work.


Feb 7, 2012 at 4:18 PM

Try this: CanSort(Allow = FilterCondition.StartsWith | FilterCondition.Equal | FilterCondition.Contains, Deny = FilterCondition.None)

The point is that each datatype has a default deny. As default the Contains operation is denied with strings, because it is not efficiently executed from the database. 
Specifying  Deny = FilterCondition.None you remove the default deny....and you can specify...on your responsibility whatever you like. However, remember that the database can use the indexes just with the startswith...not with the Contains, so the db will pass through all entries to find the ones matching your condition.

Feb 8, 2012 at 3:57 PM

Maybe my previous answer was not so clear. 

The point is that in order to add a new filtering operator you must act on the CanSort attribute that is among the attributes of the class used as grid item. In the case of the example it is the MetaToDo class that is the MetaDataType of the ToDo class.


Adding one more option to the FilterClauseSelect just show one more option in the select. User can use it, but once on the server all confitions are parse and analyzed. ALL CONDITIONS NOT ALLOWED BY THE CanSort attribute are automatically removed for security reasons. In fact a malicious user might try a denial of service attack by sending a condition that is difficult to filter (Contains is on of this ones as explained in my previous post): only condition VERIFIED TO BE ON THE CAN SORT attribute are accepted so a possible attack is rejected.

As a conclusion in the FilterClauseSelect you can just put LESS options than the ons allowed by the CanSort not MORE

Feb 8, 2012 at 4:16 PM

Thank you! It works. I changed the string fields to:

[CanSort(Allow = FilterCondition.Equal | FilterCondition.NotEqual | FilterCondition.Contains | FilterCondition.StartsWith | FilterCondition.EndsWith, Deny = FilterCondition.None)]

Feb 9, 2012 at 1:39 PM

If you like the Mvc Controls Toolkit consider doing a 5 star review on codeplex :)