ASP.NET MVC采取行动两次打电话 [英] ASP.NET MVC Action is Called Twice
问题描述
我有一个被称为两次特定的控制器动作,被奇怪的这一边是造成问题,我的应用程序的单部分(不是一个真正的问题,它只是提醒我注意双呼叫)。
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?
推荐答案
不返回假
或preventing对事件的默认操作在JavaScript单击处理程序上一个链接,使得通过AJAX调用。在这种情况下,它也将采取链接的默认操作(即非Ajax后,以相同的URL)。
Not returning false
or 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).
例
<%= 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
});
}):
这篇关于ASP.NET MVC采取行动两次打电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!