[英]ASP.NET MVC 3: Multiple Html.RenderAction() Problem
我在使用 Razor 渲染引擎(C#)時遇到了 ASP.NET MVC 3 的問題。 我在一個視圖中使用多個Html.RenderAction()
方法,並且只呈現布局。
繼續之前的一個重要說明:我不能復制任何代碼,因為我沒有這樣做的知識產權。 :(
無論如何,我注意到如果我使用以下語法@{ RenderSection("SectionName", true); }
@{ 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.)
〜/模型/實體.edmx
(Generated with Entity Framework Wizard.)
@Html.Action(...)
有效,但我真的很想知道為什么@Html.RenderAction(...)
無效。 歡迎任何建議。 :)
作為替代方案,我建議嘗試切換到:
@Html.Action("actionMethod","controller")
此擴展助手的工作方式與 RenderAction 類似,但返回 MvcHtmlString,而不是直接寫入 Output 緩沖區(響應流)。
Html.RenderAction 是一個返回 void 的方法。 所以你必須放一個“;” 在最后。 至於異常,如果您想堅持使用該輔助方法,您可能想嘗試對其進行調試並查看變量設置等。
希望這會有所幫助。
對於 RenderAction,您必須將它與所示的 bello 一樣的大括號
@{Html.RenderAction(...);}
希望這對某人有所幫助。
嘗試這個:
@section Table1
{
RenderAction("Table1", "Table");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.