Blog Stats

Meta

Posts Tagged ‘Contextual Dropdown’

There are several terms to describe the common UI technique of having 2 dropdown lists, with the selections of the first dropdown changing the options of the second. Common terms for this include Dependent Dropdowns, Context Sensitive Dropdowns, Contextual Dropdowns and probably others.

I recently had a requirement for this functionality in a control in an ASP.NET MVC project. When I searched for some samples on how to do this in JQuery, I realized that all the samples I found used AJAX to populate the second list. In this particular case, it made more sense to keep everything on the client, so that’s what I developed. This post is about how I accomplished this using JQuery, JSON.NET and a ViewModel with a couple extension methods. If you’re looking for ways to do this with AJAX, you’ll have to read one of the many other examples in the blogosphere.

The Requirement

The user needs a way to choose a color for a product. The chosen color is categorized under a parent color (Blue.Baby, Blue.Sky, Yellow.Banana, Yellow.Lemon). The user should be able to first pick a parent color and then view a list of all child colors from which to pick. The catalog of color selections is stored in a database and should be used to drive the dropdowns. The dropdowns are just a helper as the user is able to input any color name they wish, regardless of whether or not it appears in the second dropdown. Up to 5 colors can be chosen per product, so the color selection should be a control that can be repeated on a view, however, this post will ignore that requirement and show how this can be accomplished in a singular fashion.

The Object Model

The ViewModel that drives the View where this functionality resides contains an IList<VendorColor> property, which will be used to populate the dropdowns. The VendorColor class looks like this:

In order to keep everything on the client, we’ll need to get the pertinent data from the collection of VendorColor objects to the View so it’s accessible by JQuery and JavaScript. To do this, 2 extension methods were written to serialize the required data to JavaScript objects.

First, we need to gather the distinct PrimaryColor values from the list of VendorColor objects and get them into an array. Here’s what that extension method looks like.

The next extension method will be used to serialize the entire collection of VendorColor objects to JSON objects that we can later parse with JQuery and fill our dependent dropdown list. This is ridiculously easy with the help of the awesome JSON.NET library. Check out what can be done in one line of code!

Now let’s tie it all together by seeing the view and all the javascript that makes this work. It should be noted that my view is utilizing the latest version of the JQuery library.

//use vars for my controls so JQuery and Javascript won't have to retype each time
var primaryDd = '#PrimaryColor';
var secondaryDd = '#SecondaryColor';

Now the script that makes all this work, complete with comments. In the real world, this is stored in an external .js file, as it should be. This keeps the views cleaner and Google favors it for SEO. Keep in mind that the script is using the vars defined above.

JQuery and ASP.NET Ajax has made it very easy to use AJAX to accomplish dependent dropdowns. In many cases, it’s wasteful and unnecessary to go to the server for this information over and over again. In such cases it can be advantageous to do this purely on the client. Using JSON.NET to serialize objects from .NET to JSON and using JQuery and its “grep()”, “each()” and “val()” methods to get the right data and handle population and selection. This solution didn’t take too long to develop, but if you know a better way, I’d love to hear about it.