Friday, September 30, 2011

Action method calling twice in ASP.NET MVC

         This happens due to many reasons.In one my project i have faced this  was an image with an
empty “src”  property and that would call controller once again.

        This is not the issue with  ASP.NET MVC .It is browser behavior .If the response form(Web form )
having any broken div tags  (Unclosed div tags) or Image source is empty then browser thinking that
form not loaded properly and resubmit the form .It causes to call action method twice in ASP.NET MVC

    Precautions to  prevent the issue

  • Maintain all image tags having  “src” or Image URL (if control is ASP.NET ) with valid image url
  • Add alternative description for every image control (ex:  alt=”Home Banner”)
  • Check all div tags closed properly in Web page
  • Validate your each web page once the development is  completed http://validator.w3.org/

Once you specify your webpage “url”  in the Validator.w3.org site and click on check button .it shows the list of issues and description of respective error

Ex: One of the sample web page I have validated using http://validator.w3.org/ . The result you can find below  snap shot

image