Optimizely 12+ with Forms : Custom form container, FormContainerBlock Partial not found

Vote:
 

Hi

Just wanted to share something I found that might help someone else going forward or perhaps someone knows a better way of doing it. 

Im running Optimizely CMS 12+ with Razor Pages and the new "/Features" structure from Foundation  and was experiencing routing issues with the 

FormcontainerBlock partial or specific Elementblocks after installing Optimizely Forms. 

I could render the built in element types in the default FormContainer but when creating a custom FormContainer Block the application couldnt find the FormContainerBlock partial:

@await Html.PartialAsync("FormContainerBlock", Model) - Resulted in partial not found error

To fix this I had to add path: ~/FormsViews/Views/ElementBlocks/Components/{0}/{0}.cshtml to my ViewExpander since the FormContainerBlock.cshtml partial is now in below "Views/ElementBlocks/Components/FormContainerBlock/" in the Episerver.Forms module

Foundation view expander example (running MVC): https://github.com/episerver/Foundation/blob/main/src/Foundation/Infrastructure/Display/FeatureViewLocationExpander.cs 

Example:

Startup.cs

  services.AddRazorPages(opt => opt.RootDirectory = "/Features")
                .AddRazorOptions(ro => {
                    ro.ViewLocationExpanders.Add(new FormsViewLocationExpander());
                });

FormsViewLocationExpander.cs

 public class FormsViewLocationExpander : IViewLocationExpander
    {
        private readonly List<string> _viewLocationFormats = new List<string>()
        {
            "~/FormsViews/Views/ElementBlocks/{0}.cshtml",
            "~/FormsViews/Views/ElementBlocks/Components/{0}/{0}.cshtml",
        };
 
        public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context,
            IEnumerable<string> viewLocations)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
 
            if (viewLocations == null)
            {
                throw new ArgumentNullException(nameof(viewLocations));
            }
 
            var expandedLocations = viewLocations.Union(_viewLocationFormats);
            foreach (var item in expandedLocations)
            {
                yield return item;
            }
        }
 
        public void PopulateValues(ViewLocationExpanderContext context)
        {
            return;
        }
    }
#270594
Edited, Jan 26, 2022 14:47
Palle Mertz - Jan 31, 2022 11:10
Damn that helped me alot even though I am not using foundation and even viewlocation expander (imho view components shouldn't rely on magic) ...
Needed the view path for the FormContainerBlock.cshtml razor view for my custom formcontainer :D
Vote:
 

Just a heads-up, there have been a lot of updates to the NET5 version of Foundation in the past week, including some Forms-related updates. Note that it's now using the "main" branch for the official CMS 12 version (no longer using the net5 branch).

#270738
Jan 27, 2022 23:17
Vote:
 

Great saw that! 

I can see Foundation now have the views in Features/Shared/Views/ElementBlocks.

Just a curious question, is there a reason (that would be good to know) why Foundation have copied the element/forms views there instead of using the episerver form routes to the views in the forms module? Since I cant see that you have changed anything in the views. 

Couldnt you have looked in the Features shared folder first and then fallback to FormsViews? https://github.com/episerver/Foundation/blob/main/src/Foundation/Infrastructure/Display/FeatureViewLocationExpander.cs 

"/Features/Shared/Views/ElementBlocks/{0}.cshtml",

"~/FormsViews/Views/ElementBlocks/{0}.cshtml",

"~/FormsViews/Views/ElementBlocks/Components/{0}/{0}.cshtml",
#270745
Jan 28, 2022 7:34
Vote:
 

We added custom classes to the forms so they are not the same.  Also for consistency we put them in the Features/Shared

#270824
Jan 29, 2022 1:47
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.