如何从 Ajax 表单提交 asp.net mvc 中的下拉列表 [英] How do you submit a dropdownlist in asp.net mvc from an Ajax form

查看:26
本文介绍了如何从 Ajax 表单提交 asp.net mvc 中的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 ajax 表单内部的下拉列表onchange"事件提交?

How do you submit from a dropdownlist "onchange" event from inside of an ajax form?

根据以下问题:如何提交asp.net mvc 中的下拉列表,从 Html.BeginFrom 内部,您可以设置 onchange="this.form.submit" 并将下拉列表更改回来.

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.

但是,使用以下代码(在 Ajax.BeginFrom 中):

However, using the following code (inside an Ajax.BeginFrom):

<% using (Ajax.BeginForm("UpdateForm", new AjaxOptions() { UpdateTargetId = "updateText" })) { %>
    <h2>Top Authors</h2>

    Sort by:&nbsp;<%=Html.DropDownList("sortByList", new SelectList(ViewData["SortOptions"], new { onchange = "this.form.submit()" })%>

    <%= Html.TextBox("updateText")%>
<% } %>

回发到控制器操作,但整个页面被替换为updateText"文本的内容,而不仅仅是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.

因此,不是只替换 Ajax.BeginForm 内的区域,而是替换整个页面.

Thus, rather than replacing just the area inside the Ajax.BeginForm, the entire page is replaced.

下拉列表调用 this.form.submit 使得只有 Ajax.BeginForm 内的区域的正确方法是什么?

What is the correct way for the dropdownlist to call this.form.submit such that only the area inside the Ajax.BeginForm?

推荐答案

好吧,将近 2 年后,你可能不再关心了.谁知道:也许其他人(例如我 ;-) 会这样做.

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:

在您的 Html.DropDownList(...) 调用中,更改

In your Html.DropDownList(...) call, change

new { onchange = "this.form.submit()" }

new { onchange = "this.form.onsubmit()" }

你能看出区别吗?;-)

Can you spot the difference? ;-)

原因是 Ajax.BeginForm() 创建一个带有 onsubmit() 处理程序的表单来异步提交表单.通过调用 submit(),您可以绕过这个 onsubmit() 自定义处理程序.调用 onsubmit() 对我有用.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆