Asp.net-mvc – MVC Controller Called twice

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 
    }
}

Best Solution

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.

Related Question