asp.net-mvc ASP.NET MVC 操作被调用两次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1751266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC Action is Called Twice
提问by Dusda
I have a specific controller action that is being called twice, which aside from being weird is causing problems with a singleton portion of my application (not really a problem, it just brought the double call to my attention).
我有一个特定的控制器操作被调用了两次,除了奇怪之外,它还会导致我的应用程序的单例部分出现问题(不是真正的问题,它只是引起了我的注意)。
Any idea why a controller action would be executed twice every time?
知道为什么控制器动作每次都会执行两次吗?
回答by tvanfosson
Not returning falseor preventing the default action on the event in a JavaScript click handler on a link that makes the call via AJAX. In this case, it will also take the default action of the link (i.e., a non-AJAX post to the same URL).
false在通过 AJAX 进行调用的链接上的 JavaScript 单击处理程序中,不返回或阻止对事件的默认操作。在这种情况下,它还将采取链接的默认操作(即,非 AJAX 发布到同一 URL)。
Ex.
前任。
<%= Html.ActionLink( "action", "controller" ) %>
$(function() {
$('a').on('click', function(e) {
// solve with "e.preventDefault();" here
var href = $(this).attr('href');
$.get(href, function() { ... });
// or solve with "return false;" here to prevent double request
});
}):
回答by thd
I had a similar issue. The issue was caused by this line in my _Layout.cshtml file:
我有一个类似的问题。问题是由我的 _Layout.cshtml 文件中的这一行引起的:
<link rel="shortcut icon" href="">
After removing the above line, my actions are called once.
删除上述行后,我的操作将被调用一次。
回答by dellyjm
Very late but I found that having
很晚了,但我发现有
<img id="myLogo" src="#"/>
caused this to make it do the call again. Hope this helps someone, the reason I am not sure.
导致它再次拨打电话。希望这对某人有所帮助,原因我不确定。
Found solution here. http://skonakanchi.blogspot.com/2011/09/action-method-calling-twice-in-aspnet.html
在这里找到解决方案。 http://skonakanchi.blogspot.com/2011/09/action-method-calling-twice-in-aspnet.html
回答by Fitz
This is late as well but perhaps this will help someone.
这也晚了,但也许这会对某人有所帮助。
My issue was similar (controller method would fire once on initial load and then would fire again right away asynchronously).
我的问题是类似的(控制器方法会在初始加载时触发一次,然后会立即异步再次触发)。
I was able to inspect the HttpContext.request object and determined that the second request was due to Visual Studio's "Browser Link" feature.
我能够检查 HttpContext.request 对象并确定第二个请求是由于 Visual Studio 的“浏览器链接”功能引起的。
I disabled Browser Link and the mystery ajax call to my controller method no longer happens.
我禁用了浏览器链接,并且不再发生对我的控制器方法的神秘 ajax 调用。
回答by Hakan F?st?k
In my specific case, it was the loading of jquery.unobtrusive-ajax.min.jsfile for twice.
This was causing the
$(document).ready(function() { });function to be executed two times.
在我的具体情况下,它是jquery.unobtrusive-ajax.min.js两次加载文件。
这导致
$(document).ready(function() { });函数被执行两次。
Hope this is helpful.
希望这是有帮助的。
回答by Dey Chavez
I share my experience in case it serves someone,I had the following inconvenient
我分享我的经验,以防它为某人服务,我有以下不便
a submit button
一个提交按钮
<button type="submit" class="gb-btn-outline-primary margin-top-s" id="btnProcess">
<i class="fas fa-play margin-right-xs"></i> Procesar
</button>
what he called the following Javascript function
他称之为以下 Javascript 函数
$(function () {
$('#btnProcess').click(function (e) {
var plantID = $("#plantID option:selected").val();
if (plantID == "" || plantID == null || plantID == 0) {
showMessage("2", "Seleccione la planta");
e.preventDefault();
}else{
var plant = $('#plantID option:selected').text();
$("#PlantName").val(plant);
var unit = $('#UnitID option:selected').text();
$("#UnitName").val(unit);
$("#remakeAudit").val(false);
$("#frmSearch").submit();
}
});
});
the problem I had is that I called the action twice because of the click of the function and the submit in this line --> $("#frmSearch").submit();
我遇到的问题是,由于单击该函数并在此行中提交,我两次调用了该操作 --> $("#frmSearch").submit();
the solution to the problem was to avoid the event by default of the function click with e.preventDefault();
问题的解决方案是使用 e.preventDefault() 避免函数 click 的默认事件;
in the following way:
通过以下方式:
$(function () {
$('#btnProcess').click(function (e) {
e.preventDefault(); //<<---
var plantID = $("#plantID option:selected").val();
if (plantID == "" || plantID == null || plantID == 0) {
showMessage("2", "Seleccione la planta");
e.preventDefault();
}else{
var plant = $('#plantID option:selected').text();
$("#PlantName").val(plant);
var unit = $('#UnitID option:selected').text();
$("#UnitName").val(unit);
$("#remakeAudit").val(false);
$("#frmSearch").submit();
}
});
});
回答by K.L Sathish
Using kendo grid this issue happened. use grid option AutoBind(false)..
使用剑道网格发生了这个问题。使用网格选项AutoBind(false)..
回答by malik masis
I had a similar issue. The issue was caused by this line in partial view.cshtml file:
我有一个类似的问题。问题是由部分 view.cshtml 文件中的这一行引起的:
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
after removing it is fixed.
去掉后就修好了。
回答by Scott Pendleton
In my controller, method A calls method B. It was evident that they were being called twice. I verified this by writing to the output window. Interestingly, it said:
在我的控制器中,方法 A 调用方法 B。很明显,它们被调用了两次。我通过写入输出窗口来验证这一点。有趣的是,它说:
Method A was called.
Method A was called.
Method B was called.
Method B was called.
I would have expected A, B, A, B. Also, despite the double calls, I only received back XHR.statusText once. I know, because that text is added automatically to a table in the browser.
我会期待 A、B、A、B。此外,尽管有两次调用,但我只收到了一次 XHR.statusText。我知道,因为该文本会自动添加到浏览器中的表格中。
Anyway, the problem related to jquery.form.js. I use it to enable AJAX file uploading.
无论如何,与jquery.form.js相关的问题。我用它来启用 AJAX 文件上传。
The documentation says that when initializing the connection of jquery.form.js to your HTML form during $(document).ready(), you can add an option called "url". If you don't, it defaults to the ACTION of your form.
该文档说,在 $(document).ready() 期间初始化 jquery.form.js 与 HTML 表单的连接时,您可以添加一个名为“url”的选项。如果不这样做,则默认为表单的 ACTION。
I was relying on the default, and getting the double calls.
我依赖于默认值,并得到了双重调用。
The problem went away when I added the url option and removed the ACTION from the form tag.
当我添加 url 选项并从表单标签中删除 ACTION 时,问题就消失了。
回答by Envil
My problem was that I accidentally included the recaptcha/api.jsfile form google twice. One in the BaseLayout and one in the view.
我的问题是我不小心将recaptcha/api.js文件表单 google包含了两次。一个在 BaseLayout 中,一个在视图中。

