DataFilterClauseFor & FilterClauseSelect

Like the DataFilter helper also the DataFilterClause allows the Controller to receive directly a boolean LinQ expression to be used to filter some data, when the View is posted.  To this end the ViewModel exposes a property of the type Expression<TData, bool> that will be filled with a LinQ expression when the View is posted.

However, instead of defining the whole Filter each DataFilterClause helper call, defines a single Filter condition. All condtions defined by different DataFilterClauses with the same  Expression<TData, bool> parameter are combined into an unique filter expression with a logical And. If the field used in the definition of the clause is not decorated with the CanSort attribute the condition is not combined into the final filter as a defense against denial of service attacks.  The final expression is returned into the Expression<TData, bool> property exposed by the ViewModel.

The signature of the method is:


public static HtmlHelper<DataFilterClause<T, F>> DataFilterClauseFor<VM, T, F>
            (this HtmlHelper<VM> htmlHelper,
            Expression<Func<VM, Expression<Func<T, bool>>>> filter,
            Expression<Func<T, F>> field,
            string filterName)



filter is the property that will be filled with the final filter expression, field defines the property of the type T that will be constrained by the condition, and finally filterName is a name that must identify univocally the filter condition among all others that involve the same field.

The DataFilterClauseFor returns an HtmlHelper that can be used to render input controls to get the condition definition from the user. The properties of the model associated to the helper that need to be filled by the user through adequate input controls are:

  • bool Selected: True if the user would like to select the condition. Typically it is rendered with a checkbox.
  • F Search: The Value used to constrain the field of type F. Typically is rendered with a TextBox, TypedTextBox, or DateTimeInput
  • FilterCondition Condition: An enumeration that defines the operator to use in the condition (Greater, Less, StartsWith, Equal, etc.). Typically it is rendered with a Select.

The Select that lists all possible operators that can be used in the definition of the condition can be rendered with the ad hoc helper:


public static MvcHtmlString FilterClauseSelect<VM, T, F>(
            this HtmlHelper<VM> htmlHelper,
            FilterCondition value,
            Expression<Func<T, F>> field,
            IDictionary<string, object> htmlAttributes=null,
            FilterCondition conditions = 
                FilterCondition.Equal | 
                FilterCondition.LessThan | 
                FilterCondition.LessThanOrEqual |
                FilterCondition.GreaterThan |
                FilterCondition.GreaterThanOrEqual |
                FilterCondition.NotEqual |
                FilterCondition.StartsWith |
                FilterCondition.EndsWith )


 As a default, the language used for the text of the various operators are in English. However, the developer, can provide custom strings by defining them in the optional Resource file passed as argument to the Method that initialize the Mvc Controls Toolkit in the Global.asax:




For more information, please, see the section on Theming. The key to be defined are: 

FilterCondition_Equal, FilterCondition_LessThan, FilterCondition_LessThanOrEqual, FilterCondition_GreaterThan, FilterCondition_GreaterThanOrEqual, 

FilterCondition_NotEqual, FilterCondition_StartsWith, FilterCondition_EndsWith, FilterCondition_Contains, FilterCondition_IsContainedIn

The parameter conditions of the FilterClauseSelect can be obtained by intersecting the operations we would like to list with the one accepted by the column with one of the overloads of the static method CanSortAttribute.AllowedForProperty.

Below an example of use:


<div class="MvcControlsToolkit-Hover">
                            var h1 = DataFilterClauseFor(
                                        @(h1.CheckBox("Selected", h1.ViewData.Model.Selected)
                                        @(h1.FilterClauseSelect(h1, h1.ViewData.Model.Condition, column.Field)
                                    <td colspan="2">
                                        @{object val1=h1.ViewData.Model.Search;
                                          if (! h1.ViewData.Model.Selected)
                                              val1 = string.Empty;
                                        @h1.TextBox( "Search", val1)


For a complete example please download the code of the themed datagrid example included in the binary distribution which uses the DataFilterClause to allow columns filtering. See here columns filtering working.

Last edited Jun 22, 2014 at 11:53 AM by frankabbruzzese, version 6


No comments yet.