Reputation: 3367
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?
Upvotes: 26
Views: 39072
Reputation: 529
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!
Upvotes: 0
Reputation: 87
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.
Upvotes: 1
Reputation: 11
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.
Upvotes: 1
Reputation: 29
In my case I just had to add a [HttpPost]
on my action and the problem was solved.
Upvotes: 0
Reputation: 21
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();
}
});
});
Upvotes: 2
Reputation: 595
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.
Upvotes: 2
Reputation: 1037
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.
Upvotes: 0
Reputation: 51
Using kendo grid this issue happened. use grid option AutoBind(false)..
Upvotes: 1
Reputation: 19421
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.
Upvotes: 2
Reputation: 2727
My problem was that I accidentally included the recaptcha/api.js
file form google twice. One in the BaseLayout and one in the view.
Upvotes: 0
Reputation: 561
Remove null able argument from view
public ActionResult Edit(int? id)
to
public ActionResult Edit(int id)
Upvotes: 0
Reputation: 353
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.
Upvotes: 7
Reputation: 426
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
Upvotes: 12
Reputation: 2083
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.
Upvotes: 15
Reputation: 532455
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
});
}):
Upvotes: 28