当前位置 : 主页 > 网络编程 > ASP >

asp.net-mvc – N2 for MVC – 如何让Zones工作?

来源:互联网 收集:自由互联 发布时间:2021-06-24
我正在看MVC的 N2 CMS最小示例(从 here开始) 我已经弄明白了大部分内容,但我看到N2支持’部分’,你可以放入’区域’. 如何在最小的示例中使用区域和部件? Html.Zone()命令似乎没有开箱即
我正在看MVC的 N2 CMS最小示例(从 here开始)

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

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

Html.Zone()命令似乎没有开箱即用的功能.

从 libardo at the N2 forum获得一点帮助

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

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

<pages>
  <namespaces>
    ...
    <add namespace="N2.Web.Mvc.Html"/>
    ...

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

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)添加零件模型,例如这个只是将Title显示为一个字符串.请注意,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; }
        } 
    }
}

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);
        }

    }
}

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

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

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

网友评论