我可以将N2 CMS页面渲染为另一个正常MVC Razor视图的一部分吗?

时间:2016-11-03 10:11:04

标签: asp.net-mvc razor n2cms

我在N2 CMS系统中有一些操作方法页面,我想在普通MVC视图的底部包含一个或两个。我已经使用N2 API来使用以下方法为Controller中的特定页面拉回ContentItem:

ContentItem contentItem = global::N2.Context.Current.UrlParser.Parse("/support/sample-code/example-one");

现在我希望它简单到拉回这个内容项,然后要求N2在视图中渲染相关的页面/部分,但我无法找到任何文档或信息,如果这是可能或我将如何去做。

有人能指出我正确的方向吗?

感谢。 :)

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。

它几乎 IS 就像告诉N2 CMS呈现ContentItem一样简单(ish),因为你可以使用Html.DroppableZone()传递ContentPage ... ish ... < / p>

Html.DroppableZone()可以接受ContentItem和“ZoneName”(string),它将呈现指定的ZoneName,就像它在{{1 }}。这确实意味着你没有在页面中呈现页面但是ContentItem但在我们的情况下这很好,因为我们想要呈现的所有页面实际上都包含一个名为“ServiceTemplate”的部分,其中包含我们想要的所有信息在其他页面上呈现。

(虽然如果你想要整页,你可以想办法使用原始页面的View来获取部件的布局......祝你好运,因为我去了它,发现很多问题Part等...)

控制器

因此,这一切都相当令人困惑所以这里是我们的控制器:

@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); } } } N2查找该页面的UrlParser
  • 检查它是我们想要的页面类型( WE 仅在其上有ContentItem部分的页面之后)
  • 然后将ServiceTemplate传递给索引视图

仅供参考:我们将使用AJAX按需绘制,因此它只会返回部分内容......您可以使用ContentItem来绘制带有布局等的整页...

视图

现在在视图中:

@ @using N2     @model ContentItem

return View(contentItem);

视图循环通过ContentItem的子节点,并且它找到的每个子节点与我们正在寻找的@foreach (ContentItem contentItem in Model.Children) { if (contentItem.Visible && contentItem.ZoneName != null && !contentItem.IsPage && contentItem.TemplateKey == "ServiceTemplate") { @Html.DroppableZone(Model, contentItem.ZoneName).Render() } } 部件匹配,它使用ServiceTemplate函数将其传递给整页的ContentItem和ContentItem的{ {1}} {1}}。{/}

所以我说“ SIMPLE! ”;)