tags:

views:

31

answers:

2

Hi,

ASP.NET MVC application. I have a Html.DropDownList. I want to redirect to another controller when the selection is changed, and want the key of the selected dropdownlist item as a param in the URL.

How should I go about this?

Thanks

+1  A: 

You'll need to do this with Javascript; I would recommend using jquery. Also, put the dropdown in a form, method of get and action of the controller/action URL. Then set the click event to post the form.

So, your HTML will be something like:

<form id="myForm" action="/Controller/Action" method="get">
    <select id="mySelect">...</select>
</form>

And in jquery something like:

$('#mySelect').change(function() { $('#myForm').submit(); });

Note you can use the Html.Form() helper to create the form. Also, I'd recommend having a submit button on the form, which you can hide with javascript. Then people who have JS disabled still have a way to submit the form.

Chris Shaffer
+1  A: 

You have to use JavaScript to do this:

<select onchange="OnChangeEvent(this);">
 <option value="1">option 1</option>
 <option value="2">option 2</option>
</select>

<script type="text\javascript">
<!--
 function OnChangeEvent(dropDownElement){
  var selectedValue = dropDownElement.options[dropDownElement.selectedIndex].value;
  document.location = "/some/mvc/route/" + selectedValue;
 }
//-->
</script>
JonoW
If you use jQuery, use Chris Shaffers solution
JonoW