Create a Dynamic Filter ViewModel
Currently all the filters on the index pages have been the default filters provided by Dynamic MVC. We want to create a new type of filter that we will be able to use dynamically throughout the web site simply by adding an attribute to the property we want to filter. This new filter is going to be used to filter by date range. To accomplish this we will need to create three things: an attribute, viewmodel and a partial view.
We can start by creating a Dynamic Filter ViewModel. This viewmodel should inherit from DynamicFilterBaseViewModel. The viewmodel will contain all the logic for the Dynamic Filter. We will first need to create properties needed for this logic and the partial view. Create the six properties in the image above.
Override The ViewModelCreated Method
The ViewModelCreated method gets called when the view model gets instantiated by DynamicMVC. You can use this method to instantiate properties in the viewmodel that may get set on the attribute by the developer. We are going to need several properties for our date range filter such as StartLabel, EndLabel, StartQueryStringName, EndQueryStringName and AddDays. We can access these values through the controlParameters dictionary.
We also need to set the StartFilterValue property. If QueryStringStartName is found in the Route, we will set the StartFilterValue to the route value. Otherwise we will use the AddDays value passed from the attribute to set the StartFilterValue.
The EndFilterValue is also set to the route value if QueryStringEndName is found in the route. If it is not found in the route, the default value is DateTime.Now.
Override The Filter Method
The Filter method is called by the repository to perform the actual filtering. We will need to use dynamic linq to perform the filter since we do not know the type of queryable we will be filtering.
Create a Custom Dynamic Filter Attribute
We are going to create a custom attribute called DynamicFilterDateRangeAttribute. For a Dynamic Filter Attribute to fuction properly, we need to inherit from DynamicFilterUIHintAttribute. The DynamicFilterUIHintAttribute functions very similarly to FilterUIHintAttribute built into MVC. Our new custom attribute only needs a constructor. The first two values passed into the base constructor are the name of the partial view and the type of the view model. The last value is a param array of objects that is used to fill the control parameters dictionary that gets passed into the ViewModelCreated method. The param array needs to be filled out in key value pairs where the first value is a string.
Create a Custom Partial View
The partial view has the responsibility of performing the correct binding on the view. You want to make sure you are binding the form names to the names in the query string. We can do this by using the html helper and passing in the StartQueryStringName and EndQueryStringName values that are passed into the view model.
Using Our New Dynamic Filter
Now that we have completed all the work to create a new Dynamic Filter. We can now use it simply by decorating a date property with the new DynamicFilterDateRangeAttribute. In this example we set the default date range to 20 days. This results in a very efficient query being sent to the database. If we have a very large table, one with several million records, the query should perform an index seek, on an indexed date column, prior to the paging being performed.