如何从 Ajax 表单在 asp.net mvc 中提交下拉列表
如何从 ajax 表单内部的下拉列表“onchange”事件提交?
根据以下问题: How do you Submit a dropdownlist in asp .net mvc,从 Html.BeginFrom 内部,您可以设置 onchange="this.form.submit" 并更改下拉列表回发。
但是,使用以下代码(在 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")%>
<% } %>
发送回控制器操作,但整个页面将替换为“updateText”文本的内容,而不仅仅是“updateText”文本框中的内容。
因此,不是只替换 Ajax.BeginForm 内的区域,而是替换整个页面。
下拉列表调用 this.form.submit 以便仅调用 Ajax.BeginForm 内的区域的正确方法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你可以尝试这样做(需要 jQuery):
What you can try to do it this (jQuery required):
我也有同样的问题。 我在部分视图中有几个下拉列表,因此它们可以独立刷新,但设置“onchange”属性会不断刷新整个页面。
我注意到“this.form.submit()”总是指在部分视图之外的主表单。 因此,我在 AJAX 表单中添加了一个提交按钮,并提到:
我的“Model.IdIdex”只是一个用于访问同一页面中不同控件的变量。
希望能帮助到你。
I had the same problem too. I had several dropdown lists in partial views so they could refresh independently, but setting the "onchange" attribute kept refreshing the entire page.
I noticed that "this.form.submit()" always referred to the main form, outside the partial view. So instead I added a submit button inside the AJAX form and referred to that:
My "Model.IdIdex" is just a variable to access different controls in the same page.
Hope it helps.
如果您使用 MVC,那么最好的方法可能是使用 jQuery...
您的控制器将类似于:
If you are using MVC then probably the best way is with jQuery...
Your controller would be something like:
我的 AJAX.BeginForm 中有一个这样的按钮,
并且 onsubmit 或 Francisco 的解决方案不起作用(我仍然不知道为什么)
所以我创建了一个替代方案:
I had a button like this in my AJAX.BeginForm
And onsubmit or the solution from Francisco didn't work (I still don't know why)
So I created an alternative:
我们可以看看你的控制器代码吗? 如果是 Ajax 请求而不是整个视图,则可以在控制器中使用 Request.IsMvcAjaxRequest() 来仅返回部分数据。 在您的视图中将表单移动到 PartialView 并调用
Html.RenderPartial("viewname");
在您的控制器中:
if (Request.IsMvcAjaxRequest())
{
return PartialView("viewname");
}
其他
{
//此处非 Ajax 代码。
}
Can we see your Controller code? You can use Request.IsMvcAjaxRequest() in your controller to return only a portion of data if it is an Ajax Request instead of an entire View. In your View move your form to a PartialView and call
Html.RenderPartial("viewname");
In your Controller:
if (Request.IsMvcAjaxRequest())
{
return PartialView("viewname");
}
else
{
//Non Ajax code here.
}
好吧,差不多两年后,你可能不再关心了。 谁知道呢:也许其他人(比如我;-)会这样做。
因此,这是(非常简单)的解决方案:
在
Html.DropDownList(...)
调用中,更改为
Can you find the Difference? ;-)
原因是
Ajax.BeginForm()
创建一个带有onsubmit()
处理程序的表单来异步提交表单。 通过调用submit()
,您可以绕过此onsubmit()
自定义处理程序。 调用onsubmit()
对我有用。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, changeto
Can you spot the difference? ;-)
The reason is that
Ajax.BeginForm()
creates a form with anonsubmit()
handler to submit the form asynchronously. By callingsubmit()
, you bypass thisonsubmit()
custom handler. Callingonsubmit()
worked for me.在你的下拉菜单中替换
为
in your dropdown replace
to