Redirect to controller / view on list change

ASP.NET MVC Application. I have Html.DropDownList. I want to redirect to another controller when the selection is changed and I want the key to be selected in the selected dropdownlist as a URL parameter.

How should I do it?

thank

+2


source to share


2 answers


You will need to do this with Javascript; I would recommend using jquery. Also, place the dropdown in the form, getter and action of the controller / action url. Then set the click event to post the form.

So your HTML will look something like this:

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

      



And in jquery, something like:

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

      

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

+2


source


To do this, you must use JavaScript:



<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>

      

+3


source







All Articles