ASP.NET MVC采取行动两次打电话 [英] ASP.NET MVC Action is Called Twice

查看:138
本文介绍了ASP.NET MVC采取行动两次打电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个被称为两次特定的控制器动作,被奇怪的这一边是造成问题,我的应用程序的单部分(不是一个真正的问题,它只是提醒我注意双呼叫)。

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屋!

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