I'm trying to auto-save a selection in a dropdown (ASP.NET, MVC, VB), but it's not behaving as expected. Here's the dummy action in the controller:
我正在尝试在下拉列表中自动保存选择(ASP.NET,MVC,VB),但它没有按预期运行。这是控制器中的虚拟动作:
<AcceptVerbs(HttpVerbs.Post)> _
Function TestAction(ByVal id As Integer) As ActionResult
Return Content(id)
End Function
and the HTML:
和HTML:
<script type="text/javascript" src='<%= Url.Content("~/Scripts/MicrosoftAjax.debug.js") %>'></script>
<script type="text/javascript" src='<%= Url.Content("~/Scripts/MicrosoftMvcAjax.debug.js") %>'></script>
<% Using Ajax.BeginForm("TestAction", New AjaxOptions With {.UpdateTargetId = "test"})%>
<%=Html.Hidden("id", 123)%>
<%=Html.DropDownList("actions", Nothing, New With {.onchange = "this.form.submit();"})%>
<input type="submit" value="Submit" />
<span id="test"></span>
<% End Using%>
The Submit button works as expected - the span is populated with "123". The dropdown on the other hand opens a new page with nothing but "123" on it. Why "this.form.submit()" not doing the same thing as the Submit button? Is there a different call I should make to emulate the Submit button?
“提交”按钮按预期工作 - 跨度填充“123”。另一方面,下拉列表打开一个新页面,上面只有“123”。为什么“this.form.submit()”没有与Submit按钮做同样的事情?是否有一个不同的调用我应该模仿提交按钮?
2 个解决方案
#1
this.form.submit does not run the form.onsubmit event. Pressing the submit button, on the other hand, does. That, combined with the HTML that Ajax.BeginForm generates, explains why the two behave differently. As for how to make your event do the same thing as pressing the submit button, look at the HTML in the linked article:
this.form.submit不运行form.onsubmit事件。另一方面,按提交按钮可以。结合Ajax.BeginForm生成的HTML,解释了为什么两者表现不同。至于如何使您的事件与按下提交按钮的操作相同,请查看链接文章中的HTML:
Sys.Mvc.AsyncForm.handleSubmit(
this,
new Sys.UI.DomEvent(event),
{
insertionMode: Sys.Mvc.InsertionMode.replace,
updateTargetId: 'test'
});
#2
I know this is old, but there is a new (and better) way to do this.
我知道这是旧的,但有一种新的(更好的)方法可以做到这一点。
Instead of doing using javascript, use jQuery. Just had this issue and it worked great.
而不是使用javascript,使用jQuery。刚刚遇到这个问题而且效果很好。
this.form.submit()
<---- Javascript
this.form.submit()<---- Javascript
$("form").submit()
<---- jQuery
$(“form”)。submit()<---- jQuery
#1
this.form.submit does not run the form.onsubmit event. Pressing the submit button, on the other hand, does. That, combined with the HTML that Ajax.BeginForm generates, explains why the two behave differently. As for how to make your event do the same thing as pressing the submit button, look at the HTML in the linked article:
this.form.submit不运行form.onsubmit事件。另一方面,按提交按钮可以。结合Ajax.BeginForm生成的HTML,解释了为什么两者表现不同。至于如何使您的事件与按下提交按钮的操作相同,请查看链接文章中的HTML:
Sys.Mvc.AsyncForm.handleSubmit(
this,
new Sys.UI.DomEvent(event),
{
insertionMode: Sys.Mvc.InsertionMode.replace,
updateTargetId: 'test'
});
#2
I know this is old, but there is a new (and better) way to do this.
我知道这是旧的,但有一种新的(更好的)方法可以做到这一点。
Instead of doing using javascript, use jQuery. Just had this issue and it worked great.
而不是使用javascript,使用jQuery。刚刚遇到这个问题而且效果很好。
this.form.submit()
<---- Javascript
this.form.submit()<---- Javascript
$("form").submit()
<---- jQuery
$(“form”)。submit()<---- jQuery