Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Action is Called Twice

Tags:

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?

like image 692
Dusda Avatar asked Nov 17 '09 19:11

Dusda


People also ask

How many action results are there in MVC?

There are 7 types of content returning results: ViewResult. PartialViewResult. ContentResult.

Can we have duplicate methods in MVC?

MVC action methods are strictly invoked via HTTP (Hypertext Transfer Protocol). In the URL, it should not be any duplicate value of address or name.

What is action ASP NET MVC?

ASP.NET MVC Action Methods are responsible to execute requests and generate responses to it. By default, it generates a response in the form of ActionResult. Actions typically have a one-to-one mapping with user interactions.


1 Answers

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    }); }): 
like image 116
tvanfosson Avatar answered Oct 03 '22 19:10

tvanfosson