【发布时间】:2010-10-03 17:52:12
【问题描述】:
我正在查看 MVC 的 N2 CMS 最小示例(来自 here)
我已经弄清楚了大部分,但我看到 N2 支持您可以放入“区域”的“部件”。
如何让 Zones 和 Parts 在最小示例中工作?
Html.Zone() 命令似乎不能开箱即用。
【问题讨论】:
标签: asp.net-mvc n2 n2cms
我正在查看 MVC 的 N2 CMS 最小示例(来自 here)
我已经弄清楚了大部分,但我看到 N2 支持您可以放入“区域”的“部件”。
如何让 Zones 和 Parts 在最小示例中工作?
Html.Zone() 命令似乎不能开箱即用。
【问题讨论】:
标签: asp.net-mvc n2 n2cms
在libardo at the N2 forum 的帮助下
这是将区域和部件添加到 MVC 的 N2 最小示例的“最小”方式:
1) 在 web.config pages.namespaces 节点中添加这个命名空间:
<pages>
<namespaces>
...
<add namespace="N2.Web.Mvc.Html"/>
...
2) 添加一个容器页面模型,使用 AvailableZones 属性:
using N2.Integrity;
...
[Definition("ContainerPage")]
[AvailableZone("Right", "MyRightZone")]
public class ContainerPage : N2.ContentItem
{
...
3) 以通常的 N2 方式添加 Container 控制器,这里没有什么特别需要使其成为容器:
[Controls(typeof(ContainerPage))]
public class ContainerController : ContentController<ContainerPage>
{
...
4) 在容器的视图中,使用 Html.DroppableZone 函数:
<div class="n2zone">
<% Html.DroppableZone("MyRightZone").Render(); %>
</div>
5) 添加零件模型,例如这个只是将标题显示为字符串。请注意,PartDefinition 使它成为可以放入区域的部件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using N2;
using N2.Details;
namespace MyProject.Models
{
[PartDefinition("SimplePart")]
[WithEditableTitle("Title", 10)]
public class SimplePart : ContentItem
{
[DisplayableLiteral()]
public override string Title
{
get { return base.Title; }
set { base.Title = value; }
}
}
}
6) 为部件添加一个控制器。这是通常的 N2 控制器,除了我们重写 Index 以返回 PartialView:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using N2.Web;
using N2.Web.Mvc;
using MyProject.Models;
namespace MyProject.Controllers
{
[Controls(typeof(SimplePart))]
public class SimplePartController : ContentController<SimplePart>
{
public override ActionResult Index()
{
return PartialView(CurrentItem);
}
}
}
7) 最后,为 Part 控制器添加一个局部视图。这里不需要什么特别的:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyProject.Models.SimplePart>" %>
<div class="simplePart">
<%= Html.DisplayContent(m => m.Title) %>
</div>
在 N2 编辑器中,您可以将任意数量的 SimpleParts 拖放到 ContainerPage 页面中。
【讨论】: