2010-10-03 80 views
3

我期待在对MVC的N2 CMS小例子(从hereN2 for MVC - 如何让区域工作?

我已经想通了大部分,但我看到N2支持的“零件”,你可以拖放到“区域”。

如何获得区域和零件在最小示例中的工作?

Html.Zone()命令似乎不能立即使用。

回答

12

libardo at the N2 forum

一点帮助这里的添加区域和零件的N2小例子,对于MVC的“最小”的方式:

1)添加这个命名空间在web.config页面。命名空间节点:

<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,没有什么特别需要在这里,使之成为容器:

[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)为零件添加控制器。这是除了我们覆盖指数返回PartialView通常N2控制器:

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)最后,添加一个局部视图的部分控制。这里没有什么特别的需要:

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

在N2编辑器中,您可以随意地将尽可能多的SimpleParts放入ContainerPage页面。