我找到了解决问题的方法。
它几乎 IS 就像告诉 N2 CMS 呈现 ContentItem 一样简单(ish),您可以通过将 ContentPage 传递给 Html.DroppableZone() 来使用它... ish...
Html.DroppableZone() 可以接受ContentItem 和“ZoneName” (string),它将呈现指定的 ZoneName,就好像它在包含在 ContentItem 中的页面上一样。这确实意味着您没有从页面渲染页面而是 Part 但在我们的情况下这很好,因为我们要渲染的所有页面实际上都包含一个名为“ServiceTemplate”的部分,其中包含我们要渲染的所有信息在其他页面上。
(虽然如果你想要整页,你可能会想出一种方法来使用原始页面的视图来获取部分的布局......祝你好运,因为我尝试了一下,发现了很多@section main{...} 等问题...)
控制器
所以这一切都相当混乱,所以这里是我们的控制器:
using System.Linq;
using System.Web.Mvc;
using N2;
namespace Data8.Website.N2.Controllers
{
public class ServiceDocsController : Controller
{
// GET: ServiceDocs
// Returns the specified service doc (partial view for AJAX)
public ActionResult Index(string url)
{
// Get the Content Item for that N2 Page (if it is an N2 Page!)
ContentItem contentItem = Context.Current.UrlParser.Parse(url);
// Ensure we found a valid N2 Content Page that contains a Service Template as one of it's parts... (these are all we will render!)
if (contentItem == null || !contentItem.IsPage || contentItem.Children.All(ci => ci.TemplateKey != "ServiceTemplate"))
{
return new HttpNotFoundResult("Page doesn't exist or doesn't contain a Service Template Part!");
}
// Return the partial view of that contentItem
return PartialView(contentItem);
}
}
}
它:
- 获取所需页面的 URL
- 使用
N2 UrlParser 查找该页面的ContentItem
- 检查是不是我们想要的页面类型(我们只在带有
ServiceTemplate 部分的页面之后)
- 然后将
ContentItem 传递给索引视图
仅供参考:我们将使用 AJAX 按需绘制它,因此它只返回部分......您可以使用 return View(contentItem); 来绘制带有布局等的完整页面......强>
查看
现在在视图中:
@@使用 N2
@model 内容项
@foreach (ContentItem contentItem in Model.Children)
{
if (contentItem.Visible && contentItem.ZoneName != null && !contentItem.IsPage && contentItem.TemplateKey == "ServiceTemplate")
{
@Html.DroppableZone(Model, contentItem.ZoneName).Render()
}
}
视图循环通过 ContentItem 的子项,并且对于它找到的与 ServiceTemplate 匹配的每个子项,我们正在寻找它使用 Html.DroppableZone() 函数将整个页面的 ContentItem 和 ContentItem 的 ZoneName 传递给它来呈现它ServiceTemplate.
所以正如我所说的“简单!”;)