微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 指定布局时,MVC控制器操作多次调用

我遇到一个问题,控制器动作被调用至少两次.我有一个视图,它有另一个视图作为其布局页面,并且视图被多次调用.如果我删除布局的规范,那么操作将始终执行一次.我浏览了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”;
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>

这是页脚页面

<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回到了同一页面.一旦我用“”替换它,它现在工作正常.

原文地址:https://www.jb51.cc/csharp/101053.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐