簡體   English   中英

指定布局時多次調用 MVC 控制器操作

[英]MVC Controller Action Gets Called Multiple Times When Layout Is Specified

我遇到了一個問題,控制器操作至少被調用兩次。 我有一個視圖,它的布局頁面有另一個視圖,並且該視圖被多次調用。 如果我刪除了布局的規范,那么該操作將始終執行一次。 我瀏覽了 StackOverflow 和其他網站,但找不到與我的特征相同的問題,所以我發布了一個新問題。

_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();
    }  

這似乎是一個簡單的問題,我肯定遺漏了一些明顯的東西。 我已經在這個網站上發布了示例項目: https : //skydrive.live.com/#cid=F2DAB940147490B0&id=F2DAB940147490B0%21140

任何想法有什么問題?

謝謝

更新:這是視圖:@{ ViewBag.Title = "TestMVCProject"; 布局 = 空; }

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

這是頁腳頁面:

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

更新:@Tieson T:謝謝,我將其更改為 Html.Partial 而不是 RenderPage。 但是問題仍然存在,因為操作方法仍然被調用兩次......(編輯描述,因為我沒有添加評論的權限)

我解決了這個問題。 代碼中有這一行:

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

src返回到同一頁面。 一旦我用""替換它,它現在就可以正常工作了。

我也面臨同樣的問題。 Action 渲染了兩次。 唯一的問題是

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

不知道圖像的 src 屬性是什么原因導致在頁面上執行兩次渲染。

我只是將其更改為“”`

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

現在工作正常。

由於您的_Footer.cshtml視圖只是普通的舊 HTML,因此絕對沒有理由調用@RenderPage()將其插入到您的布局中。 使用@Html.Partial()代替:

@Html.Partial("_Footer")

老實說,我不確定為什么布局會被調用兩次,但我假設@RenderPage() (我從來不需要)呈現整個 HTML 頁面並注入結果。 您必須在瀏覽器中檢查頁面源以進行確認。

哈。

@Url.Content(...) 導致模型被我調用兩次。 也許其他一些問題也是如此

當我們提供圖像路徑時也會發生這種情況,但是該路徑上不存在圖像。

如果以上都不像我,請嘗試禁用傳送帶擴展。

那是我的噩夢。

請享用...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM