[英]MVC Controller Action Gets Called Multiple Times When Layout Is Specified
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.我浏览了 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();
}
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我已经在这个网站上发布了示例项目: https : //skydrive.live.com/#cid=F2DAB940147490B0&id=F2DAB940147490B0%21140
Any ideas what's wrong?任何想法有什么问题?
Thanks谢谢
Update: Here's the view: @{ ViewBag.Title = "TestMVCProject";更新:这是视图:@{ 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 </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.更新:@Tieson T:谢谢,我将其更改为 Html.Partial 而不是 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)但是问题仍然存在,因为操作方法仍然被调用两次......(编辑描述,因为我没有添加评论的权限)
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. src
返回到同一页面。 Once I replace it with a ""
it works fine now.一旦我用""
替换它,它现在就可以正常工作了。
I also faced same problem.我也面临同样的问题。 The Action was rendering twice. Action 渲染了两次。 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.不知道图像的 src 属性是什么原因导致在页面上执行两次渲染。
I just changed it to ""`我只是将其更改为“”`
<img id="blah" src="" alt="your image" />
and it is working fine now.现在工作正常。
Since your _Footer.cshtml
view is just plain old HTML, there is absolutely no reason to call @RenderPage()
to insert it into your layout.由于您的_Footer.cshtml
视图只是普通的旧 HTML,因此绝对没有理由调用@RenderPage()
将其插入到您的布局中。 Use @Html.Partial()
instead:使用@Html.Partial()
代替:
@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.老实说,我不确定为什么布局会被调用两次,但我假设@RenderPage()
(我从来不需要)呈现整个 HTML 页面并注入结果。 You'd have to check the page source in your browser to confirm.您必须在浏览器中检查页面源以进行确认。
HTH.哈。
@Url.Content(...) is causing the model to get called twice for me. @Url.Content(...) 导致模型被我调用两次。 Maybe some others issue as well也许其他一些问题也是如此
当我们提供图像路径时也会发生这种情况,但是该路径上不存在图像。
guy's if none of the above works like me, try disabling Conveyor extension.如果以上都不像我,请尝试禁用传送带扩展。
that mas my nightmare.那是我的噩梦。
enjoy...请享用...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.