ASP.NET MVC Action is Called Twice

Viewed 36920

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?

15 Answers

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.

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

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

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.

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.

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

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.

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!

Related