Layla
Layla

Reputation: 47

ASP.NET MVC: Click on a button to redirect based on drop down list's value

Can anyone help me on how to create a drop down list to redirect in ASP.NET? When click on the "Create" button, the current view should be redirected to different views based on the drop down list's value.

For example, when I select on "1" from the list then click on the Create button, it should do something like: Response.Redirect("~/Drama/Create");

My drop down list code is:

<div class="jumbotron">
  <form action="/Home" method="get">
    <fieldset>
      Movie Type: <select id="MovieType" name="MovieType">
        <option value="Action">0</option>
        <option value="Drama">1</option>
        <option selected="selected" value="Comedy">2</option>
        <option value="Science Fiction">3</option>
      </select>
      <p><input type="submit" value="Submit" /> </p>
    </fieldset>
  </form>
</div>

Upvotes: 2

Views: 1470

Answers (1)

Shyju
Shyju

Reputation: 218732

Listen to the form submit event /button click of the submit button and read the selected option value and build the url and do a redirect

$(function(){

  $("form").submit(function(e){

    e.preventDefault(); // prevent default form submit behaviour

    var selectedItem = $("#MovieType").val();
    var url = "@Url.Content("~")"+selectedItem + "/create";
    alert(url);
    window.location.href=url;

  })

})

Upvotes: 3

Related Questions