Dynamic MVC Order Entry Tutorial - Create Custom Dynamic Filter

This tutorial has not been updated to version 3.0.
Sun, 5 Oct. 2014 - Chris Perry

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.

COMMENTS Add Comment
  • All things coededirns, this is a first class post
    ZikmGw542e | 3/30/2016 7:46:14 AM
  • Back in school, I'm doing so much lenrgian. http://ejvxkebtqvm.com [url=http://umjscaoo.com]umjscaoo[/url] [link=http://lugxydkkyw.com]lugxydkkyw[/link]
    q3JkQQvXzBU | 4/8/2016 9:04:43 AM
  • Привет всем! интересный у вас сайт! Нашел обширную инфу: https://zen.yandex.ru/media/planetanews/pojar-dlivshiisia-1074-dnia-potushili-iadernym-vzryvom-neveroiatnye-proekty-sssr-5ca225d83aaf7b00b2c2e2cd
    EdgarLiz | 4/5/2019 7:32:10 PM