【发布时间】:2011-09-29 06:22:00
【问题描述】:
我遇到了带有 Razor 渲染引擎 (C#) 的 ASP.NET MVC 3 的问题。我在一个视图中使用了多个Html.RenderAction() 方法,并且只呈现了布局。
继续之前的重要说明:我无法复制任何代码,因为我没有这样做的知识产权。 :(
无论如何,我注意到,如果我使用以下语法 @{ RenderSection("SectionName", true); } 而不是 @RenderSection("SectionName", true),则会出现一个通用异常,它只是说它无法使用似乎说可能存在的堆栈跟踪来呈现这些部分一些异步问题。在这两者之间,我使用的是同步控制器,所以也许这是一个线索,但我对同步/异步控制器以及何时应该在某些情况下使用它们知之甚少。
为了将所有内容放在上下文中,这是我在代码/伪代码/站点结构中尝试做的事情......
~/View/Shared/_ViewStart.cshtml
(Selects a layout according to certain conditions.)
~/View/Shared/_Layout1.cshtml
<! doctype HTML>
<html>
<head>
<!-- some irrelevant content here... -->
</head>
<body>
<div id="page">
<div id="header">
@RenderSection("Header", true)
</div>
<div id="content">
<div id="main1">
@RenderSection("Table1", true)
@RenderSection("Table2", true)
</div>
<div id="main2">
@RenderSection("Content", true)
</div>
</div>
<div id ="footer">
@RenderSection("Footer", true)
</div>
</div>
</body>
</html>
~/View/Shared/_Layout2.cshtml
(another layout)
~/View/Controller1/Action1.cshtml
@section Header
{
@RenderPage("~/Views/Shared/Sections/Header")
}
@section Footer
{
@RenderPage("~/Views/Shared/Sections/Footer")
}
@section Table1
{
@{ RenderAction("Table1", "Table"); }
}
@section Table2
{
@{ RenderAction("Table2", "Table"); }
}
@section Content
{
@{ RenderAction("Action", "Content"); }
}
~/View/Controller1/Action2.cshtml
(similar to Action1.cshtml)
~/Utilities/ModelManager.cs
public abstract class ModelManager : Controller
{
//Some useful code for controllers here...
}
~/Controller/Controller1.cs
public class Controller1 : ModelManager
{
#region Get Methods
public ViewResult Action1()
{
return View();
}
public ViewResult Action2()
{
return View();
}
#endregion
#region Post Methods
public ViewResult Action1(FormCollection form)
{
return View();
}
public ViewResult Action2(FormCollection form)
{
return View();
}
#endregion
}
~/Controller/Controller2.cs
(another controller similar to Controller1)
~/Controller/Table.cs
(Only important thing to note is that the actions are returning PartialViewResult.)
~/Controller/Content.cs
(Only important thing to note is that the action is returning PartialViewResult.)
~/Model/Entities.edmx
(Generated with Entity Framework Wizard.)
* 编辑 *
@Html.Action(...) 有效,但我真的很想知道为什么 @Html.RenderAction(...) 无效。欢迎任何建议。 :)
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-3 web-applications razor