Error create an Image widget in Kentico 12 MVC

mark json asked on March 8, 2022 12:23

I unable create an image widget with custom controller.

I copied all souce code used to build Image widget in Dancing Goat example. There is no error from my source code, but I got this error message after I click Image widget from Kentico CMS:

An error has occurred. If the problem persists, contact your system administrator.

I don't know where error accurred with my Image widget, no error happen if I click another widgets as: Text or Number widget.

After copied Image widget in Dancing Goat example, I just custom my controller as below:

using System;
using System.Linq;
using System.Web.Mvc;

using CMS.DocumentEngine;

using Kentico.PageBuilder.Web.Mvc;

using News.Controllers.Widgets;
using News.Models.Widgets;
using News.Models.Widgets.ImageWidget;

[assembly: RegisterWidget("News.Widgets.ImageWidget", typeof(ImageWidgetController), "Photo", Description = "Add a Photo", IconClass = "icon-picture")]

namespace News.Controllers.Widgets
{
    public class ImageWidgetController : WidgetController<ImageWidgetProperties>
    {

        /// <summary>
        /// Creates an instance of <see cref="ImageWidgetController"/> class.
        /// </summary>
        /// <param name="outputCacheDependencies">Output cache dependencies.</param>

        /// <summary>
        /// Creates an instance of <see cref="ImageWidgetController"/> class.
        /// </summary>
        /// <param name="propertiesRetriever">Retriever for widget properties.</param>
        /// <param name="currentPageRetriever">Retriever for current page where is the widget used.</param>
        /// <param name="outputCacheDependencies">Output cache dependencies.</param>
        /// <remarks>Use this constructor for tests to handle dependencies.</remarks>
        public ImageWidgetController(IComponentPropertiesRetriever<ImageWidgetProperties> propertiesRetriever, ICurrentPageRetriever currentPageRetriever) 
            : base(propertiesRetriever, currentPageRetriever)
        {
        }


        public ActionResult Index()
        {
            var properties = GetProperties();
            var image = GetImage(properties);

            return PartialView("Widgets/_ImageWidget", new ImageWidgetViewModel
            {
                Image = image
            });
        }


        private DocumentAttachment GetImage(ImageWidgetProperties properties)
        {
            var page = GetPage();
            return page?.AllAttachments.FirstOrDefault(x => x.AttachmentGUID == properties.ImageGuid);

        }
    } 
}

Basically, I only remove CacheDependencies.

Can someone help me?

Recent Answers


Liam Goldfinch answered on March 8, 2022 12:43

Have you checked the Event Log module in Kentico? See if there are any errors logged related to adding the widget to the page.

You can also check the browser network tab in dev tools, see the request Kentico makes when adding the widget to the page, there might be a relevant error in the response.

1 votesVote for this answer Mark as a Correct answer

mark json answered on March 8, 2022 12:57

@Liam Goldfinch thanks so much! After check my browser network request, I removed ImageWidgetController method. It works.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.