Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Controller Called twice

Tags:

asp.net-mvc

I have a Product controller with Index action, which basically creates the view form for post and Index (Post action verb) action on the ProductController which basically save the product to db but when validation errors occur, I am returning a View(mymodel) else when saved, I am returning RedirectToAction("Created,"Product") but for some odd reason when I break into the code , it is hitting the Product Controller action twice rather than just once. Hence the product has 2 records instead of one.

public ActionResult Index()
{
    return View()
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection fc)
{
    // 2 calls are made to this controller
    try
    {
        // save the product
        return RedirectToAction("Created"); 
    }
    catch(Exception ex)
    {
        // recreate the model from form collection
        return View(viewData); // when a validation error occurs it comes into the catch block 
    }
}
like image 555
chugh97 Avatar asked Jul 01 '09 12:07

chugh97


People also ask

Can you have more than one controller in MVC?

In Spring MVC, we can create multiple controllers at a time. It is required to map each controller class with @Controller annotation.

CAN controller have multiple views?

Controller is the boss, so a Controller decides which View to be rendered and Views does not / cannot care which Controller requested the View. You can / will absolutely have multiple Views from a Controller.

What is controller in MVC with example?

A controller determines what response to send back to a user when a user makes a browser request. A controller is just a class (for example, a Visual Basic or C# class). The sample ASP.NET MVC application includes a controller named HomeController. cs located in the Controllers folder.


3 Answers

Sometimes I have found Firebug to cause this behavior. Try disabling its Script panel, if you have it installed.

Explanation: In some cases Firebug isn't able to get the script sources for the display within its Script panel. In these cases it initiates a second request to get them. See issue 7401 for some discussion about this, which alleviates the problem and is fixed with Firebug 2.0.2.

like image 193
mxmissile Avatar answered Oct 06 '22 05:10

mxmissile


Here's a basic checklist (copied from here):

  1. Check that you don’t have any image or another elements in the View with an empty src attribute (<img src=”" /> for example) or have src attribute referencing something that no longer exists. You better check directly in the browser's “Page Source”, than in the View itself due to the possibility of some “dynamic” issues when the View is rendered. Once you find such empty element in the page's HTML source its usually trivial to find the same element in your View and fix the issue. This can also happen with <link href="">.

  2. Check that you don’t have any AJAX calls referencing an empty URL (browsers will interpret such empty URL as the current page and will request the current page again making the Controller action execute few times).

  3. You forgot to return “false” from the JavaScript click event handler for a link or button that makes an AJAX call. If you forget to “return false”, the browser simply interprets the default action of the link – regular, non AJAX, calling the same page again)

  4. Sometimes Firebug and YSlow [ Firefox (FF) plugins ] can cause such issues. Just temporarily disable them in FF or test with a different browser.

  5. Watch out for duplicate filters decorating your controller or action. (this was my problem)

like image 28
Leniel Maccaferri Avatar answered Oct 06 '22 05:10

Leniel Maccaferri


Another solution for this case..

I had exactly same problem, running and testing from Chrome. I couldn't debug it because the second call was coming from (external call). I have randomly tested it in Firefox and Internet Explorer where there was no double hit.

Whatever nasty thing it was, I have deleted Chrome cache (everything!!!) and problem has been resolved.

Hope it will help some of you :)

like image 22
Vaclav Elias Avatar answered Oct 06 '22 06:10

Vaclav Elias