首页 > 解决方案 > 如何在 Kentico 12 Page Builder 中创建 MVC 小部件

问题描述

上一个关于上下文的问题(上一个问题无处可去,所以我创建了这个新问题以重新开始):Unable to Create New MVC Widget in Kentico 12

我正在尝试创建一个名为“带有摘要的图像”的小部件。现在,我只是想向它添加一个属性(一个具有富文本编辑器的摘要属性)。

当我向页面构建器添加新小部件时,它没有显示为小部件选项:

带有小部件对话框的页面构建器

从这里,您可以看到我已经正确配置了页面构建器(已经添加了“富文本”小部件),您可以看到添加小部件是可能的(“富文本”小部件来自我安装的 NuGet 包称为“Kentico.EMS12.MvcComponents.Widget.RichText”),您可以看到我只有两个可用的小部件(“表单”和“富文本”),这两个都不是我要添加的小部件。

我需要您的帮助来弄清楚为什么我的新小部件没有出现在此对话框中。

这是 Visual Studio 中的解决方案资源管理器,显示了我为这个新小部件创建的所有文件:

Visual Studio 解决方案资源管理器显示与带有摘要小部件的图像相关的文件

这是我的属性类的样子:

// ImageWithSummaryProperties.cs
namespace RhythmAgency.KenticoWebsite.Widgets.ImageWithSummary
{
    using Kentico.PageBuilder.Web.Mvc;

    public class ImageWithSummaryProperties : IWidgetProperties
    {
        public string Summary { get; set; }
    }
}

这是我的视图模型的样子:

// ImageWithSummaryViewModel.cs
namespace RhythmAgency.KenticoWebsite.Widgets.ImageWithSummary
{
    public class ImageWithSummaryViewModel
    {
        public string Summary { get; set; }
    }
}

这是我的控制器的样子:

// ImageWithSummaryController.cs
using System.Web.Mvc;
using Kentico.PageBuilder.Web.Mvc;
using RhythmAgency.KenticoWebsite.Widgets.ImageWithSummary;

[assembly: RegisterWidget(
    identifier: "Rhythm.ImageWithSummary",
    controllerType: typeof(ImageWithSummaryController),
    name: "Image with Summary",
    Description = "An image with summary text.",
    IconClass = "icon-l-img-3-cols-3")]

namespace RhythmAgency.KenticoWebsite.Widgets.ImageWithSummary
{
    public class ImageWithSummaryController : WidgetController<ImageWithSummaryProperties>
    {
        public ActionResult Index()
        {
            var properties = GetProperties();
            return PartialView("Widgets/_Rhythm.ImageWithSummary", new ImageWithSummaryViewModel()
            {
                Summary = properties.Summary
            });
        }
    }
}

这是我的看法:

@* _Rhythm.ImageWithSummary.cshtml *@
@using Kentico.PageBuilder.Web.Mvc
@using RhythmAgency.KenticoWebsite.Widgets.ImageWithSummary
@using Kentico.Components.Web.Mvc.InlineEditors
@model ImageWithSummaryViewModel

@if (Context.Kentico().PageBuilder().EditMode)
{
    Html.Kentico().RichTextEditor(nameof(ImageWithSummaryProperties.Summary), null);
}
else
{
    <div class="fr-view">
        @Html.Raw(Html.Kentico().ResolveRichText(Model.Summary))
    </div>
}

我不会过多地关注视图文件,因为我什至无法将小部件添加到页面构建器,所以视图甚至没有机会被调用。

这是我的主视图文件:

@* Views/Home/Index.cshtml *@
@using Kentico.PageBuilder.Web.Mvc
@using Kentico.Web.Mvc

<h1>Rhythm Agency</h1>

@Html.Kentico().EditableArea("main")

我真的不知道为什么这个小部件没有出现在要添加到页面部分的可用小部件列表中。这里有一些额外的上下文:

编辑:

有人很好奇为什么这个问题和上一个问题不同,所以这个编辑澄清了这一点。上一个问题是关于我试图仅使用属性类来实现的小部件。这个较新的问题使用了不同的方法(即使用控制器),这是在 Kentico 中实现小部件的另一种方法。

编辑#2:

有人建议我将小部件注册程序集属性放在App_Start文件夹中,我这样做了,但没有帮助:

小部件注册码

所以为什么这不起作用仍然是一个谜。

标签: kenticokentico-12kentico-mvc

解决方案


要识别控制器和小部件,您需要将控制器放在“/Controllers”文件夹中。我的小部件控制器位于“/Controllers/Widgets”文件夹中。

我遇到的问题包括没有在类名中添加后缀“Controller”以及小部件控制器不在“/Controllers”文件夹中的问题。

另外你不是在一个单独的项目中工作吗?因为这需要您在“AssemblyInfo.cs”中使用以下内容

using CMS;
[assembly: AssemblyDiscoverable]

并确保您已在您的 kentico 项目中启用页面构建器功能。例如:

protected void Application_Start()
{
    ...

    // Gets the ApplicationBuilder instance
    // Allows you to enable and configure Kentico MVC features
    ApplicationBuilder builder = ApplicationBuilder.Current;

    // Enables the preview feature
    builder.UsePreview();

    // Enables the page builder feature
    builder.UsePageBuilder();

    ...
}

推荐阅读