How to filter using a dropdownlist

May 1, 2012 at 5:22 AM

let's say you create a general filter as you have described in the data filtering tutorial ( http://mvccontrolstoolkit.codeplex.com/wikipage?title=DataFilter). But you want to filter data using a DropDownList. So in mygeneral filter I created have :

public IEnumerable<SelectListItem> Categories { get; set; }

public string SelectedCategory;

in index.aspx, I would use:

<% var filter = Html.DataFilterBuilder(m => m.linkFilter, new MygeneralFilter());%>

Now my question is how do I use HtmlHelper returned by the DataFilterBuilder to render the DropDownList for filtering?

Coordinator
May 1, 2012 at 9:39 AM
Edited May 1, 2012 at 9:45 AM

As you use any other helper....

<%: filter.DropDownFor(m => m.MySelection.....

Where MySelection is a property you have to define in your general filter an that will contain the value of the selected item. Then you use the MySelection in your FilterBuilder to define a filtering condition based on it,

The point is that you defined just Categories....that is the list of all possible selections...what you have to put in your filter is the selected item that you define like this:

<%: filter.DropDownFor(m => m.MySelection.....

That is you use filter as the normal Html of your view....but filter will propose you the properties you defined in your general filter instead of the ones defined in the ViewModel of your View. This will cause the values of the controls to be bound to the properties of your general filter...then you can process them with the help of the FilterBuilder class. please download the BasicTutorialCode filein the download area. It contains examples of building a filter based either on the GenerraFilter or on The FilterClauseFor helper or on a mix of them