【问题标题】:Can I render an N2 CMS page as part of another normal MVC Razor view?我可以将 N2 CMS 页面呈现为另一个正常 MVC Razor 视图的一部分吗?
【发布时间】:2016-11-03 10:11:03
【问题描述】:

我在 N2 CMS 系统中存在一些操作指南页面,我想在正常 MVC 视图的底部包含其中的一两个。我已经设法使用 N2 API 来拉回控制器中特定页面的 ContentItem 使用:

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

现在我希望它就像拉回此内容项然后要求 N2 在视图中呈现相关页面/部分一样简单,但我无法找到任何文档或信息,如果这是可能或者我会怎么做。

谁能指出我正确的方向?

谢谢。 :)

【问题讨论】:

    标签: asp.net-mvc razor n2cms


    【解决方案1】:

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

    它几乎 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.

    所以正如我所说的“简单!”;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 2018-12-20
      • 2018-10-20
      • 2022-01-17
      相关资源
      最近更新 更多