In an ASP .NET MVC Razor view, I have a dropdown list as follows:
@Html.DropDownListFor(model => model.SelectedDeviceModel, Model.DeviceModelList)
DeviceModelList is just a SelectList.
How can I dynamically fill the DeviceModelList depending on a client side action like a button click or another drop down selection using Javascript/jQuery/Ajax?
You could externalize this dropdown into a partial:
then in your main view include it inside some container:
then you could have a controller action which takes some parameter and based on its value it renders this partial:
Now the last part is to send the AJAX request to refresh the drop down list when some event occurs. For example when the value of some other ddl changes (or something else, a button click or whatever):
Another possibility consists into using JSON. Your
Foocontroller action would only return some Json object containing the new key/value collection and in the success callback of the AJAX request you would refresh the drop down list. In this case you don’t need to externalize it into a separate partial. For example:and finally your Foo controller action will return Json:
For a similar example you may take a look at the following answer.