Nesting widgets in MVC

Aaron Macdonald asked on December 19, 2022 09:39

Hello

We have a requirement in K13 MVC .NET framework for an editable nested accordion. This would be best achieved for editors by nesting widget areas.

In the following post Trevor Fayas says that K13 does not support this by default:

https://devnet.kentico.com/questions/nesting-widget-zones

We have spent some time investigating his Partial Widget Page module which he recommends here:

https://github.com/KenticoDevTrev/PartialWidgetPage/tree/KX12KX13FullFramework

Unfortunately we haven't been able to get it to work, and we're not sure if it's currently functional.

Does anyone have a simple working example of this module for .NET framework?

Failing that can anyone advise on a good alternative to nesting widgets?

TIA

Correct Answer

Aaron Macdonald answered on December 22, 2022 00:23

The following is for anyone who may be struggling with the same problem in .NET framework.

For our nested accordion requirement, we now have an editable area which handles the outer accordion, and a widget inserted within that which handles the inner accordion.

That widget has a page selector property which refers to a separate page type (with a controller) which has its own editable area for building the inner accordion.

To enable the PartialWidgetPage.Kentico.MVC package, we only had to register this single service:

builder.RegisterType<PartialWidgetPageHelper>().As<IPartialWidgetPageHelper>();

The widget view is as follows. It's just a case of using the html helpers to change the context to the aforementioned separate page type and then back again:

@model ComponentViewModel<InnerAccordionWidgetProperties>

@{        
    var selectedNodeGuid = Model.Properties.InnerAccordionPage?.FirstOrDefault()?.NodeGuid;

    if (selectedNodeGuid != null)
    {
        var Context = Html.GetCurrentContext();

        int docId = Html.GetDocumentIDByNode(selectedNodeGuid.Value);

        Html.ChangeContext(docId);

        Html.RenderAction("Index", "InnerAccordionPage");

        Html.RestoreContext(Context);
    }
    else
    {
        <p>Select an inner accordion page in the widget settings...</p>
    }
}
0 votesVote for this answer Unmark Correct answer

Recent Answers


Trevor Fayas answered on December 19, 2022 16:48

Hey Aaron.

What's the error with the KX13 full framework? Sadly when we went to KX13, we (basically all us MVPs) have stopped developing any functionality for .Net 4.8 KX13 MVC, and focus only on .Net "Core" (.net 3.1+)

The partial widget page does work for the core version, i haven't tested heavily for the .net 4.8 version again because you shouldn't really be building KX13 sites in anything but the .Net 5 minimum (.net 6 current recommended).

I can try to help you figure out what the issue is, otherwise i would highly recommend refactoring the site in .net 6, and then using the latest partial widget page packages.

0 votesVote for this answer Mark as a Correct answer

Aaron Macdonald answered on December 20, 2022 00:10 (last edited on December 20, 2022 00:35)

Hi Trevor good to hear from you again.

I'm not sure why a decision was made to use framework, but there's no chance of refactoring at this stage.

We've installed the package and added service registrations, except for the following line which doesn't work and we're not sure if it's required:

//Register Dependencies for Cache, pass in any assemblies you wish to hook up
DependencyResolverConfig.Register(autofacBuilder, new Assembly[] { typeof(ApplicationConfig).Assembly, typeof(PartialWidgetPageHelper).Assembly });

The Partial Widget Page will appear in a list of allowed widgets, but if we try to insert it nothing happens, and there is nothing in the event log.

A simple working example would be helpful, maybe even something tacked onto the Dancing Goat project.

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on December 20, 2022 02:53

The widget is only an example usage along with the partial widget page type. What you'll want to do is make a simple page with a single widget zone that will be your footer, and follow instructions on https://github.com/KenticoDevTrev/PartialWidgetPage/tree/KX13-Net48MVC5 ? That had instructions for .net 48, including preserving current context, changing the context to the footer (then place your widget zone within that context) and then restore the context.

Registering dependency injection usually is through either autofac or castle Windsor.

0 votesVote for this answer Mark as a Correct answer

Aaron Macdonald answered on December 20, 2022 04:18

Unfortunately we did spend a long while trying to follow those instructions for .NET 4.8 but could not get a working outcome.

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on December 20, 2022 04:22

Sorry, I'm on vacation, I know it does work, bit it's not through the widget, you have to just the html helpers and set it up right. Maybe when I'm back in January I can take a look on a call?

0 votesVote for this answer Mark as a Correct answer

Aaron Macdonald answered on December 20, 2022 06:49

Thanks. After playing around with the html helpers and avoiding any of the 'partial widget page widget' instructions we do seem to have something somewhat working.

It wasn't clear that these were two separate approaches being described, we though they were both required.

I'll endeavour to update if we're successful. Have a nice vacation!

0 votesVote for this answer Mark as a Correct answer

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