如何从Ajax表单中提交asp.net mvc中的下拉列表 [英] How do you submit a dropdownlist in asp.net mvc from an Ajax form
问题描述
根据以下问题,如何从asp.net mvc 中提交一个下拉列表,从Html里面。 BeginFrom可以设置onchange =this.form.submit,然后更改下拉列表。
但是,使用以下代码(在Ajax.BeginFrom内):
<%using(Ajax.BeginForm(UpdateForm,new AjaxOptions(){UpdateTargetId =updateText})){ %GT;
< h2>顶级作者< / h2>
排序:& nbsp;<%= Html.DropDownList(sortByList,新的SelectList(ViewData [SortOptions],新的{onchange =this.form.submit() })%>
<%= Html.TextBox(updateText)%>
<%}%>
回到控制器的动作,但整个页面被替换为updateText文本的内容,而不仅仅是updateText内部的内容。
<
因此,而不是仅替换Ajax.BeginForm中的区域,整个页面将被替换。
下拉列表调用this.form.submit的正确方法是什么,以便只有Ajax.BeginForm中的区域?
好的,差不多2年了,你可能不在乎了,谁知道:也许别人(比如我; - )做。
(非常简单)的解决方案:
在你的 Html.DropDownList(...)
p>
new {onchange =this.form.submit()}
to
new {onchange =this.form.onsubmit()}
你能发现差异吗? ; - )
原因是 Ajax.BeginForm()
创建一个 onsubmit()
处理程序以异步方式提交表单。通过调用 submit()
,你绕过这个 onsubmit()
自定义处理程序。调用 onsubmit()
为我工作。
How do you submit from a dropdownlist "onchange" event from inside of an ajax form?
According to the following question: How do you submit a dropdownlist in asp.net mvc, from inside of an Html.BeginFrom you can set onchange="this.form.submit" and changing the dropdown posts back.
However, using the following code (inside an Ajax.BeginFrom):
<% using (Ajax.BeginForm("UpdateForm", new AjaxOptions() { UpdateTargetId = "updateText" })) { %>
<h2>Top Authors</h2>
Sort by: <%=Html.DropDownList("sortByList", new SelectList(ViewData["SortOptions"], new { onchange = "this.form.submit()" })%>
<%= Html.TextBox("updateText")%>
<% } %>
Posts back to the controller action, but the entire page is replaced with the contents of the "updateText" text, rather than just what is inside the "updateText" textbox.
Thus, rather than replacing just the area inside the Ajax.BeginForm, the entire page is replaced.
What is the correct way for the dropdownlist to call this.form.submit such that only the area inside the Ajax.BeginForm?
OK, nearly 2 years later, you probably don't care anymore. Who knows: Maybe others (such as me ;-) do.
So here's the (extremely simple) solution:
In your Html.DropDownList(...)
call, change
new { onchange = "this.form.submit()" }
to
new { onchange = "this.form.onsubmit()" }
Can you spot the difference? ;-)
The reason is that Ajax.BeginForm()
creates a form with an onsubmit()
handler to submit the form asynchronously. By calling submit()
, you bypass this onsubmit()
custom handler. Calling onsubmit()
worked for me.
这篇关于如何从Ajax表单中提交asp.net mvc中的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!