問題の解決策を見つけました。
N2 CMS にContentPageを渡すことで使用できるように ContentItem をレンダリングするように指示するのと同じくらい簡単です... ishHtml.DroppableZone()
...
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
Index ビューに渡します
参考: AJAX を使用してこれをオンデマンドで描画するので、部分的なものしか返されません...return View(contentItem);
代わりに、レイアウトなどでページ全体を描画するために使用できます...
意見
ビューで:
@ @using N2 @model 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 の Children をループし、探している Part に一致する子を見つけるたびHtml.DroppableZone()
に、ページ全体の ContentItem とZoneName
thisの ContentItem を渡す関数を使用してレンダリングしますServiceTemplate
。
私が言ったように「シンプル!」;)