在MVC下拉列表中选择调用特定的动作 [英] Invoking particular action on dropdown list selection in MVC
问题描述
我有一个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运行时错误:对象不支持属性或方法
"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?
推荐答案
您的方法应该工作。您遇到的问题是你的这个
在的onchange
事件无效使用。试着像这样的东西更换您的 this.form
引用:
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();"
} ) %>
这篇关于在MVC下拉列表中选择调用特定的动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!