如何从 Ajax 表单在 asp.net mvc 中提交下拉列表

发布于 2024-07-09 14:33:05 字数 877 浏览 12 评论 0原文

如何从 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:&nbsp;<%=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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

蘑菇王子 2024-07-16 14:33:06

你可以尝试这样做(需要 jQuery):

$("#yourDropdown").change(function() {
  var f = $("#yourForm");
  var action = f.attr("action");
  var serializedForm = f.serialize();
  $.post(action, serializedForm,
    function () { alert("Finished! Can do something here!") });
});

What you can try to do it this (jQuery required):

$("#yourDropdown").change(function() {
  var f = $("#yourForm");
  var action = f.attr("action");
  var serializedForm = f.serialize();
  $.post(action, serializedForm,
    function () { alert("Finished! Can do something here!") });
});
猛虎独行 2024-07-16 14:33:06

我也有同样的问题。 我在部分视图中有几个下拉列表,因此它们可以独立刷新,但设置“onchange”属性会不断刷新整个页面。

我注意到“this.form.submit()”总是指在部分视图之外的主表单。 因此,我在 AJAX 表单中添加了一个提交按钮,并提到:

<%=Html.DropDownList("data", ViewData["data"] as SelectList
, new { onchange = "$(\"#button" + Model.IdIndex + "\").click();" })%>


<input type="submit" id="button<%=Model.IdIndex %>" style="display: none"  /><br />

我的“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:

<%=Html.DropDownList("data", ViewData["data"] as SelectList
, new { onchange = "$(\"#button" + Model.IdIndex + "\").click();" })%>


<input type="submit" id="button<%=Model.IdIndex %>" style="display: none"  /><br />

My "Model.IdIdex" is just a variable to access different controls in the same page.
Hope it helps.

仙女山的月亮 2024-07-16 14:33:06

如果您使用 MVC,那么最好的方法可能是使用 jQuery...

<%= Html.DropDownList("sortByList", new SelectList(ViewData["SortOptions"]) %> 
<%= Html.TextBox("updateText") %>

<script>
$("#sortByList").change(function() {
    $.ajax({
        url: <%= Url.Action("UpdateForm")%>,
        type: "POST",
        data: { 'sortBy': $(this).val() },
        dataType: "json",
        success: function(result) { $('#updateText').text(result); },
        error: function(error) { alert(error); }
    })

});
</script>

您的控制器将类似于:

public JsonResult UpdateForm(string sortBy)
{
    string result = "Your result here";
    return Json(result);
}

If you are using MVC then probably the best way is with jQuery...

<%= Html.DropDownList("sortByList", new SelectList(ViewData["SortOptions"]) %> 
<%= Html.TextBox("updateText") %>

<script>
$("#sortByList").change(function() {
    $.ajax({
        url: <%= Url.Action("UpdateForm")%>,
        type: "POST",
        data: { 'sortBy': $(this).val() },
        dataType: "json",
        success: function(result) { $('#updateText').text(result); },
        error: function(error) { alert(error); }
    })

});
</script>

Your controller would be something like:

public JsonResult UpdateForm(string sortBy)
{
    string result = "Your result here";
    return Json(result);
}
或十年 2024-07-16 14:33:06

我的 AJAX.BeginForm 中有一个这样的按钮,

  <button id="submitButton" type="submit"  class="btn" style="vertical-align: top"><i class="icon"></i> replace</button>

并且 onsubmit 或 Francisco 的解决方案不起作用(我仍然不知道为什么)

所以我创建了一个替代方案:

  new { onchange = "document.getElementById('submitButton').click()" }

I had a button like this in my AJAX.BeginForm

  <button id="submitButton" type="submit"  class="btn" style="vertical-align: top"><i class="icon"></i> replace</button>

And onsubmit or the solution from Francisco didn't work (I still don't know why)

So I created an alternative:

  new { onchange = "document.getElementById('submitButton').click()" }
盗梦空间 2024-07-16 14:33:06

我们可以看看你的控制器代码吗? 如果是 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.
}

っ〆星空下的拥抱 2024-07-16 14:33:05

好吧,差不多两年后,你可能不再关心了。 谁知道呢:也许其他人(比如我;-)会这样做。

因此,这是(非常简单)的解决方案:

Html.DropDownList(...) 调用中,更改

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

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

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, 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.

人生百味 2024-07-16 14:33:05

在你的下拉菜单中替换

this.form.submit()

$(this.form).submit();

in your dropdown replace

this.form.submit()

to

$(this.form).submit();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文