N2 for MVC - 如何让Zones工作?

cod*_*ike 3 asp.net-mvc n2 n2cms

我正在看MVC 的N2 CMS最小示例(从这里开始)

我已经弄明白了大部分内容,但我看到N2支持'部分',你可以放入'区域'.

如何在最小的示例中使用区域和部件?

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

cod*_*ike 12

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

这是将区域和部件添加到MVC的N2最小示例的"最小"方法:

1)在web.config pages.namespaces节点中添加此命名空间:

<pages>
  <namespaces>
    ...
    <add namespace="N2.Web.Mvc.Html"/>
    ...
Run Code Online (Sandbox Code Playgroud)

2)使用AvailableZones属性添加Container页面模型:

using N2.Integrity;
...

[Definition("ContainerPage")]
[AvailableZone("Right", "MyRightZone")]
public class ContainerPage : N2.ContentItem
{
   ...
Run Code Online (Sandbox Code Playgroud)

3)以通常的N2方式添加Container控制器,这里没有什么特别需要使它成为一个容器:

[Controls(typeof(ContainerPage))]
public class ContainerController : ContentController<ContainerPage>
{
    ...
Run Code Online (Sandbox Code Playgroud)

4)在容器的视图中,使用Html.DroppableZone函数:

<div class="n2zone">
  <% Html.DroppableZone("MyRightZone").Render(); %>
</div>
Run Code Online (Sandbox Code Playgroud)

5)添加零件模型,例如,这个只显示标题为字符串.请注意,PartDefinition是使其成为可以放入区域的Part的原因:

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; }
        } 
    }
}
Run Code Online (Sandbox Code Playgroud)

6)为零件添加控制器.这是通常的N2控制器,除了我们重写索引以返回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);
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

7)最后,为Part控制器添加局部视图.这里没有什么特别之处:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyProject.Models.SimplePart>" %>
<div class="simplePart">
  <%= Html.DisplayContent(m => m.Title) %>
</div>
Run Code Online (Sandbox Code Playgroud)

在N2编辑器中,您可以将任意数量的SimplePart拖放到ContainerPage页面中.