Search code examples
asp.net-mvcdrop-down-menuasp-net-mvc-1

Invoking particular action on dropdown list selection in MVC


I have a dropdown list in an MVC view. On selection change of dropdown list I want to call specific action method in the controller.

What I have done on view is this :

<%=Html.DropDownList("ddl", ViewData["AvailableList"] as SelectList,
  new { onchange = "this.form.action='MyMethod';this.form.submit();" })%>

Everything is getting compiled. But a runtime exception is thrown when I change the drop down selection, as

"Microsift JScript Runtime Error : Object doesn't support property or method"

How can I redirect to specific action on list selection change event?
How can I pass parameters to this action method?


Solution

  • Your approach should work. The issue you're encountering is your use of this in your onchange event is invalid. Try replacing your this.form references with something like this:

    <%= Html.DropDownList(
            "ddl", ViewData["AvailableList"] as SelectList, 
            new { onchange = @"
                var form = document.forms[0]; 
                form.action='MyMethod';
                form.submit();" 
            } ) %>