25

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?

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
Dusda
  • 3,347
  • 5
  • 37
  • 58
  • 2
    I'll post code shortly if it's needed; I was just fishing for any general 'gotcha's' that anyone might have to offer. – Dusda Nov 17 '09 at 19:47
  • 1
    Have a look at the requests going from your browser to the server. Do you see one or two requests? If one: post some code here, if two: see tvanfosson's answer. – Jørn Schou-Rode Nov 17 '09 at 19:48

15 Answers15

28

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

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
   });
}):
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
15

I had a similar issue. The issue was caused by this line in my _Layout.cshtml file:

<link rel="shortcut icon" href="">

After removing the above line, my actions are called once.

thd
  • 2,023
  • 7
  • 31
  • 45
  • I edit all images so now they have alt property and additionally I removed src="#" but I have this issue yet. Any idea appreciated. – QMaster Jan 08 '15 at 10:13
  • Try to comment out all img tags in your view. If your action is still being called twice, then try to comment out all link tags. – thd Jan 12 '15 at 13:14
  • 2
    Thanks, that fixed my issue. To add more on how - the blank href was being assigned with the URL of the action that was actually called, and was making a call of its own. Removing this link fixed the issue for me. – Chait May 05 '16 at 14:57
12

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

dellyjm
  • 426
  • 1
  • 5
  • 10
7

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.

I disabled Browser Link and the mystery ajax call to my controller method no longer happens.

How to disable Browser Link

Fitz
  • 353
  • 4
  • 7
2

In my specific case, it was the loading of jquery.unobtrusive-ajax.min.js file for twice.
This was causing the $(document).ready(function() { }); function to be executed two times.

Hope this is helpful.

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
2

I had a similar issue. The issue was caused by this line in partial view.cshtml file:

<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

after removing it is fixed.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
malik masis
  • 487
  • 1
  • 6
  • 15
2

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

$(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();

the solution to the problem was to avoid the event by default of the function click with e.preventDefault();

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();
        }
        });
    });
Dey Chavez
  • 21
  • 2
1

Using kendo grid this issue happened. use grid option AutoBind(false)..

  • Have you find any reason behind it because I have a problem in add record so when we add a new record what happens it will add record we add logic to redirect to index page instead of that it will call again add method. – Kaval Patel Nov 20 '18 at 13:25
1

In my case, i have twice html element with same id in same view document. please check your html page (or view page) for same id attribute and clean same id name.

Amin Golmahalleh
  • 3,585
  • 2
  • 23
  • 36
1

In my case I noticed that every time I would start typing the url, the browser preloaded the page. When I then hit enter, it would get called again. The solution was simply to check if it was running, and then prevent the method call, if it was.

Robert Gutke
  • 87
  • 1
  • 5
0

Remove null able argument from view

public ActionResult Edit(int? id)

to

public ActionResult Edit(int id)
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Muhammad Waqas
  • 511
  • 6
  • 9
0

My problem was that I accidentally included the recaptcha/api.js file form google twice. One in the BaseLayout and one in the view.

Envil
  • 2,687
  • 1
  • 30
  • 42
0

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:

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.

Anyway, the problem related to jquery.form.js. I use it to enable AJAX file uploading.

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.

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.

Scott Pendleton
  • 1,021
  • 3
  • 16
  • 32
0

In my case I just had to add a [HttpPost] on my action and the problem was solved.

0

In my case, it was because of "AdBlock" chrome extension, if anyone encounters this problem be sure you made a test on incognito mode of browser!

Project Mayhem
  • 439
  • 5
  • 12