MVC 的 N2 - 如何让区域工作?

发布于 2024-09-26 02:06:39 字数 291 浏览 9 评论 0原文

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

我已经弄清楚了大部分内容,但我发现 N2 支持“部分”,您可以将其放入“区域”中。

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

Html.Zone() 命令似乎无法开箱即用。

I'm looking at the N2 CMS Minimal Example for MVC (from here)

I've figured out most of it, but I see that N2 supports 'Parts' that you can drop into 'Zones'.

How do I get Zones and Parts working in the minimal example?

The Html.Zone() command doesn't seem to work out-of-the-box.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘味果▽酱 2024-10-03 02:06:39

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 方式,这里没有什么特殊需要使它成为一个容器:

[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 编辑器中,您可以将任意数量的 SimplePart 放入 ContainerPage 页面中。

With a bit of help from libardo at the N2 forum

Here's the 'minimal' way of adding Zones and Parts to the N2 Minimal Example for MVC:

1) Add this namespace in the web.config pages.namespaces node:

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

2) Add a Container page model, using the AvailableZones attribute:

using N2.Integrity;
...

[Definition("ContainerPage")]
[AvailableZone("Right", "MyRightZone")]
public class ContainerPage : N2.ContentItem
{
   ...

3) Add Container controller in the usual N2 manner, nothing special needed here to make it a container:

[Controls(typeof(ContainerPage))]
public class ContainerController : ContentController<ContainerPage>
{
    ...

4) In the view for the container, use the Html.DroppableZone function:

<div class="n2zone">
  <% Html.DroppableZone("MyRightZone").Render(); %>
</div>

5) Add a part model, e.g. this one just shows Title as a string. Note that PartDefinition is what makes it a Part that can be dropped into a Zone:

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) Add a Controller for the Part. This is the usual N2 controller except that we override Index to return a 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) Finally, add a partial view for the Part controller. Nothing special is needed here:

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

In the N2 editor you can then drop as many SimpleParts as you like into the ContainerPage pages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文