Ale*_*rty 7 c# asp.net-mvc web-applications razor asp.net-mvc-3
我面临着使用Razor渲染引擎(C#)的ASP.NET MVC 3的问题.我Html.RenderAction()
在视图中使用多个方法,只渲染布局.
继续之前的一个重要注意事项:我无法复制任何代码,因为我没有这样做的知识产权.:(
无论如何,我注意到如果我使用以下语法@{ RenderSection("SectionName", true); }
而不是@RenderSection("SectionName", true)
有一个通用异常,只是说它无法呈现带有堆栈跟踪的部分,似乎可能存在一些异步问题.之间,我正在使用同步控制器,所以这可能是一个主角,但我对同步/异步控制器以及何时应该在某些情况下使用它们知之甚少.
把所有内容放在上下文中,这是我在代码/伪代码/站点结构中尝试做的...
〜/查看/共享/ _ViewStart.cshtml
(Selects a layout according to certain conditions.)
Run Code Online (Sandbox Code Playgroud)
〜/查看/共享/ _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>
Run Code Online (Sandbox Code Playgroud)
〜/查看/共享/ _Layout2.cshtml
(another layout)
Run Code Online (Sandbox Code Playgroud)
〜/查看/控制器1/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"); }
}
Run Code Online (Sandbox Code Playgroud)
〜/查看/控制器1/Action2.cshtml
(similar to Action1.cshtml)
Run Code Online (Sandbox Code Playgroud)
〜/公共事业/ ModelManager.cs
public abstract class ModelManager : Controller
{
//Some useful code for controllers here...
}
Run Code Online (Sandbox Code Playgroud)
〜/控制器/ 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
}
Run Code Online (Sandbox Code Playgroud)
〜/控制器/ Controller2.cs
(another controller similar to Controller1)
Run Code Online (Sandbox Code Playgroud)
〜/控制器/ Table.cs
(Only important thing to note is that the actions are returning PartialViewResult.)
Run Code Online (Sandbox Code Playgroud)
〜/控制器/ Content.cs
(Only important thing to note is that the action is returning PartialViewResult.)
Run Code Online (Sandbox Code Playgroud)
〜/型号/ Entities.edmx
(Generated with Entity Framework Wizard.)
Run Code Online (Sandbox Code Playgroud)
的@Html.Action(...)
工作,但我真的想知道为什么@Html.RenderAction(...)
没有工作.欢迎任何建议.:)
Dax*_*x70 13
作为替代方案,我建议尝试切换到:
@Html.Action("actionMethod","controller")
Run Code Online (Sandbox Code Playgroud)
此扩展助手的工作方式与RenderAction类似,但返回MvcHtmlString,而不是直接写入Output缓冲区(Response流).
Html.RenderAction是一个返回void的方法.所以你必须放一个";" 在末尾.至于异常,如果你想坚持使用那个帮助器方法,你可能想尝试调试它并看看变量设置为等等.
希望这会有所帮助.