通过AJAX加载ASP.NET MVC的表单提交多次 [英] ASP.NET MVC Form loaded via ajax submits multiple times

查看:698
本文介绍了通过AJAX加载ASP.NET MVC的表单提交多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一个ajax形式的局部视图。这部分观点通过Ajax调用加载到我的网页。我可以编辑的字段,提交表单,一切工作正常。不过,如果我重新加载形式N次,表单将被点击保存按钮时提交N次了。

这里是code的局部视图....

  @model等等等等......<脚本的src =@ Url.Content(〜/脚本/ jQuery的-1.4.4.min.js)TYPE =文/ JavaScript的>< / SCRIPT>
<脚本的src =@ Url.Content(〜/脚本/ jQuery的-ui.min.js)TYPE =文/ JavaScript的>< / SCRIPT>
&LT;脚本src=\"@Url.Content(\"~/Scripts/jquery.unobtrusive-ajax.min.js\")\"type=\"text/javascript\"></script>
&LT;脚本的src =@ Url.Content(〜/脚本/ jquery.validate.min.js)TYPE =文/ JavaScript的&LT; / SCRIPT&GT;
&LT;脚本src=\"@Url.Content(\"~/Scripts/jquery.validate.unobtrusive.min.js\")\"type=\"text/javascript\"></script>&LT; D​​IV ID =MODALFORM&GT;
  @using(Ajax.BeginForm(编辑,信息,新{ID = Model.UserId},{AjaxOptions =的onSuccessinfoUpdate}))
  {
       //表单字段GO HERE      &LT;输入类型=提交值=保存/&GT;
  }
&LT; / DIV&GT;

我在做什么错误导致此行为?


解决方案

每个重新加载表格时,将触发器放置提交表单

。因此,你有N个提交,如果你重新加载形式n次。

尝试加载的形式只有一次,如果可能的话。

您可以尝试解除提交触发器,当你点击你的提交按钮:

 &LT;输入类型=提交值=保存的onClick =submitForm()/&GT;VAR submitForm =功能(){
    $(#formAddressShipping表)的触发器(提交)。
    $(#formAddressShipping表)解除绑定(提交)。
    返回false;
};

I have a partial view containing an ajax form. This partial view is loaded onto my page via an ajax call. I can edit the fields and submit the form and everything works normally. However, if I reload the form N times, the form will submit N times when the save button is clicked.

here is the code for the partial view....

@model blah blah...

<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")"type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript</script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"type="text/javascript"></script>

<div id="modalForm">
  @using (Ajax.BeginForm("Edit", "Info", new{id = Model.UserId}, AjaxOptions{OnSuccess = "infoUpdate" }))
  {


       //FORM FIELDS GO HERE

      <input type="submit" value="Save" />


  }
</div>

What am I doing wrong that is causing this behavior?

解决方案

Each time you reload the form, a trigger is placed for submitting the form. Thus you have n submitting, if you reload the form n times.

Try to load the form only once, if it's possible.

You can try to unbind the submit trigger, when you click on your submit button:

<input type="submit" value="Save" onClick="submitForm()" />

var submitForm = function() {
    $("#formAddressShipping form").trigger('submit');    
    $("#formAddressShipping form").unbind('submit');
    return false;
};

这篇关于通过AJAX加载ASP.NET MVC的表单提交多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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