I'm new to MVC, converting an existing WPF application to an MVC web site. One common function that we use across the application multi-stage dropdown selection. By that I mean:
- Dropdown A is a list from the database
- After selecting A, Dropdown B is populated based on that selection
- After selecting B, Dropdown C is populated based on that selection
In WPF I could make a custom control that made it easy to drop this in wherever I needed it, but I'm not sure the MVC partial views will give me the power I need to do it, since it seems that putting javascript in a partial view is not recommended or is not even possible. I'd like to do ajax calls back to the controller to load the data for each dropdown as they are selected, which would require javascript.
One idea I've tossed around is to have a partial view that takes a model to load Dropdown A, and then do a @Scripts.Render('multiDropdown.js'
right below the Partial()
. I don't love this idea because instead of just rendering the partial, I now have to ensure that I'm also including the scripts. I also dislike this because if there was some way to put the javascript into a partial i could also provide ViewData
to give the dropdowns ID names to use in the javascript, so I could have more than one of the partials on the same page. If the javascript is kept separate I'd have to hard-code IDs on the elements in the partial and the javascript.
Another thought I had was to use the @Ajax
helper to try and load them "without" javascript, but I'm not sure if, because of the javascript limitations in partials, if the @Ajax
methods will work properly in partial views either.
Are the other techniques that I could use to solve this problem? This sort of filtering seems like a common type of functionality, but I couldn't find any resources that would help me achieve it, especially in a re-usable manner.