MVC Controller Action Gets Called Multiple Times When Layout Is Specified

20,715

Solution 1

I figured out the problem. There was this line in the code:

<img alt="TestMVCProject Logo" src="#" width="350" height="150" id="TestMVCProjectLogo" />

The src was going back to the same page. Once I replace it with a "" it works fine now.

Solution 2

I also faced same problem. The Action was rendering twice. The only problem was with

<img id="blah" src="#" alt="your image" /> 

Dont know for what reason src attribute of image was causing to perform rendering twice on page.

I just changed it to ""`

<img id="blah" src="" alt="your image" />

and it is working fine now.

Solution 3

@Url.Content(...) is causing the model to get called twice for me. Maybe some others issue as well

Solution 4

It also happens when we are giving a image path, but, image does not exist on that path.

Solution 5

Since your _Footer.cshtml view is just plain old HTML, there is absolutely no reason to call @RenderPage() to insert it into your layout. Use @Html.Partial() instead:

@Html.Partial("_Footer")

I honestly don't know for sure why the layout is called twice, but I assume @RenderPage() (which I've never had a need for) renders an entire HTML page and injects the results. You'd have to check the page source in your browser to confirm.

HTH.

Share:
20,715
user1717575
Author by

user1717575

Updated on July 09, 2022

Comments

  • user1717575
    user1717575 almost 2 years

    I'm having a problem where a controller action is called at least twice. I have a view that has another view as its layout page, and the view is called multiple times. If I remove the specification for the layout then the action is consistently executed once. I looked through StackOverflow and other sites but couldn't find a problem which had the same characteristics as mine so I'm posting a new question.

    _ViewStart.cshtml:
    @{
       Layout = "~/Views/Shared/_ProfileLayout.cshtml";
    }
    
    Index.cshtml inside my Profile folder: @{
    ViewBag.Title = "Index";    
    }
    Index
    
    Controller Action:
    public ActionResult Index()
        {            
            //ToDo: BusinessLogic
            //This method gets called twice
            //******************//
            return View();
        }  
    

    It seem's like a simple problem and I'm definitely missing something obvious. I've posted the sample project on this site: https://skydrive.live.com/#cid=F2DAB940147490B0&id=F2DAB940147490B0%21140

    Any ideas what's wrong?

    Thanks

    Update: Here's the view: @{ ViewBag.Title = "TestMVCProject"; Layout = null; }

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>TestMVCProject</title>
        <link rel="Stylesheet" type="text/css" href="../../Content/ActiveSite.css" />    
        <link href="../../Content/themes/TestMVCProject/jquery-ui-1.9.1.custom.min.css" rel="stylesheet" type="text/css" />
        <script src="../../Scripts/jquery-1.8.2.js" type="text/javascript"></script>
        <script src="../../Scripts/jquery-ui-1.9.1.custom.js" type="text/javascript"></script>
    </head>
    
    <body>
    <div class="container">
      <div class="header">  
      <div id="loginimagecontainer">
      @using (Html.BeginForm("LoginActions", "Account", FormMethod.Post, new { @id = "LoginActionsForm" }))
      {  
      @Html.AntiForgeryToken()      
      <a href="#"><img src="/images/icons/message-icon.png" width="60" height="60" alt="Messages" title="Messages"/></a>
      <a href="/Account/LogOff"><img src="/images/icons/log-out-icon.png" width="60" height="60" alt="Log off" title="Log off"/></a>
      }    
      </div>
    
      <div class="logotext"><img alt="TestMVCProject Logo" src="#" width="350" height="150" id="TestMVCProjectLogo" /></div>
        </div>    
      <div class="content profile">  
        <div id="leftPane">
            <img src="#" alt="Placeholder" width="165" height="200" id="ProfilePhoto" title="Profile Photo" />
            <div id="Username"></div>
            <div id="NavLinks">
                <div class="ProfileNavigation" onclick="Navigate('/Profile/Update')"><span>Profile</span><img src="/images/icons/edit-icon.png" width="30" height="30" alt="Profile" /></div>
                <div class="ProfileNavigation"><span>Search</span><img src="/images/icons/search-icon.png" width="30" height="30" alt="Search" /></div>
                <div class="ProfileNavigation" onclick="Navigate('/Photo')"><span>Photos</span><img src="/images/icons/camera-icon.png" width="30" height="30" alt="Photos"/></div>
            </div>
        </div>
          <div id="adcontainer">
            <h4>Ads go here</h4>
            <p>content goes here</p>    
            </div>
    
        <div id="centerPane">
        @RenderBody()
        </div>     
    
      </div>
      @RenderPage("~/Views/Shared/_Footer.cshtml")
        <div id="redirectiondialog" class="dialog">
        <br />
        Hey, wait up... we're redirecting you over to the login page
        <br />
        </div> 
    
        <script type="text/javascript">    
            function Navigate(url) {
                window.location = url;
                return false;
            }
        </script>
    </div>
    </body>
    </html>
    

    and here's the footer page:

     <div class="footer">
        <div class="fltrt">Copyright 2012 TestMVCProject Inc&nbsp;</div>
        <p><a href="/Profile/Test">About</a> | <a href="#">Contact</a> | <a href="#">FAQ</a> | <a href="#">Advertise</a> | <a href="#">Support</a> | <a href="#">Feedback</a> | <a href="#">Login</a> | <a href="#">Register</a> | <a href="#">Privacy</a> | <a href="#">Terms</a></p>    
    </div>
    

    Update: @Tieson T: Thanks, I changed this to be Html.Partial instead of RenderPage. However the problem still persists as the action method still gets called twice... (Editing description as I don't have privileges to add comments)

    • Tieson T.
      Tieson T. over 11 years
      What does your layout view look like?
    • user1717575
      user1717575 over 11 years
      I just updated the description and added this information now
  • Steve Owen
    Steve Owen over 11 years
    Maybe mark this as the answer if you can as it's still showing up as an unanswered question.
  • tntwyckoff
    tntwyckoff over 8 years
    wow, wow, wow. what a PIA that was. in my case someone had embedded dozens of these throughout the codebase; copy/paste no doubt. this would never have occurred to me. it's funny that because the browser only loads each resource once the issue was kind of masked; if it had been 10 calls it would have been way less subtle and more detectable. thanks for posting your findings!!
  • Deshani Tharaka
    Deshani Tharaka over 6 years
    <iframe src="#" ></iframe> may have this issue