3

我正在查看 MVC 的N2 CMS最小示例(来自此处

我已经弄清楚了大部分,但我看到 N2 支持你可以放入“区域”的“部件”。

如何让区域和部件在最小示例中工作?

Html.Zone()命令似乎不是开箱即用的。

4

1 回答 1

12

在 N2 论坛上得到 libardo 的一些帮助

这是将区域和部件添加到 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 页面中。

于 2010-10-04T11:27:13.930 回答