如何从一个Ajax表单提交在asp.net的MVC一个DropDownList [英] How do you submit a dropdownlist in asp.net mvc from an Ajax form

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

问题描述

如何从一个DropDownList的onchange事件从一个ajax形式提交里面?

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

根据以下问题:你如何提交DropDownList的在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.

然而,使用以下code(一个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.

这是对的dropdownlist调用Ajax.BeginForm

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

推荐答案

OK,近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(...)通话,改变

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

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

你能发现有什么区别? ; - )

Can you spot the difference? ;-)

原因是, Ajax.BeginForm()创建了一个具有的onsubmit()处理表单提交异步形成。通过调用提交,你绕过这个的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一个DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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