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?
From the controller action, you're going to make the call the same way:
Once you do that, put a Javascript function on your page that calls the method. How you call it, however, is going to depend on whether it's an AJAX call or not. That is, whether you want a page round-trip or not. For the non-AJAX call:
If it's an AJAX call:
To pass parameters to the action, you just need to make sure that all of the data elements you want to pass are contained within the
<form>
tag. For example, let's say you want to include a first and last name with your drop down list. In the view, you'll do something like this:In the Javascript function:
yes you can and here is the code for that:
Do you really need to submit the form? You could redirect:
where
redirect
is a custom defined function:This is a jQuery thing. Give it a class and wire up to it.
A rough script might look like this:
Like both : D
I propose a mix -I like a lot the jQuery.
Your approach should work. The issue you're encountering is your use of
this
in youronchange
event is invalid. Try replacing yourthis.form
references with something like this: