I am building a screen in my C# Asp.Net MVC application, which has a few related drop downs.So, for example, I select ‘Category’, and then a 2nd drop down should display all related sub categories for the selected category.
So, would the model contain a List, used to build the Category drop down, and then a List, initially empty. On the selection of the category, I need to do a post back, passing back the id if the selected Category, then populate the List<> Sub Categories… and then build the Sub Category drop down?
Hopefully someone can assist with the design of my model. The model would also have quite a few non-list related data for the item being edited. And I think the model would also need a ‘CategorySelectedId’, so that I know what was selected?
The nicest way is to use AJAX for this. You’ll have to hook the
changeevent to your first select box, and once it is changed, you’ll do a AJAX request with the selected value to someAction. The action will return a JSON list, which will be parsed and put in the nextselect box.Update
The model for the JSON returning action can be an anonymous type really, or a
IEnumerableofSelectListItem. UseLinq: `mycollection.select(item=> new SelectListItem() { Name = item.Name, Value = item.ID.ToString() });If we assume the page looks like this:
Your model would look like:
Of course you could add validation attributes etc.