Try our conversational search powered by Generative AI!

Model.Form null when trying to preview FormContainerBlock

Vote:
 

So I am implementing Episerver forms for a customer. Their PreviewBlockController looks like so:

[TemplateDescriptor(
        Inherited = true,
		Tags = new string[] { RenderingTags.Preview },
		TemplateTypeCategory = TemplateTypeCategories.MvcController)]
	public class PreviewBlockController : ActionControllerBase, IRenderTemplate
    {
		public ActionResult Index(IContent currentBlock)
		{
            const string masterLayoutFull = "_BlockEditLayout";

			// Setup edit mode for any block
			ViewBag.BlockInEditMode = EPiServer.Web.Routing.Segments.RequestSegmentContext.CurrentContextMode == ContextMode.Edit;

			var baseType = currentBlock.GetType().BaseType;

		    if (baseType == typeof (TeaserBlock))
		        return View("~/Views/Blocks/TeaserBlock/Edit.cshtml", masterLayoutFull, new TeaserBlockViewModel(currentBlock as TeaserBlock));

Naturally Ill insert something like

if (baseType == typeof(FormContainerBlock))
		    {
                return View("~/Views/Shared/ElementBlocks/FormContainerBlock.ascx", currentBlock as FormContainerBlock);
		    }

The problem is that currentBlock.Form == null which makes the form render with the message

"<%--in case formcontainerblock is used as a property, we cannot build form model so we show a warning message to notify user--%>" (its from this bug, not applicable to my case I think: LINK)

The forms renders well (Form prop is populated) when its rendered in a contentarea on a page, both on the site and in edit mode (on the page).

Anyone have any idea of whats up here?

(I tried creating a new preview controller inheriting from FormContainerBlockController as seem to work here LINK but with the same error, I also tried using fetching the block from the database with Icontentloader with the same effect)

#171709
Edited, Nov 15, 2016 16:13
Vote:
 

I "solved" it by just instansiating a random Form (IForm) which works in this case since I just wanted to render the form in edit mode, I dont think this should be marked as "solved" just yet though.

#171839
Nov 18, 2016 13:26
Vote:
 

Hi,

Form (IForm) model is the pure data model, keep information about the form, relation to its steps, and relation to its elements. It is beneficial to separate the logic of Form in Edit Mode (using Blocks) and that in ViewMode for rendering.

This model can be built independently. With current implementation of Forms, we use Episerver Block as Form, Steps, and Elements. They are all nature blocks. They are organized as steps with "ordering convention" (Elements after a Step belong to that step).

Step1

 - E11

 - E12

Step2

 - E21

To build that "model" from "Blocks implementation", we have 

Injected<FormBusinessService> _formBusinessService;

_formBusinessService.Service.BuildFormModel(formContainerBlock);


Honestly, this might be considered as a current LIMITATION, we need to call BuildFormModel() before accessing Form property. We see that property FormContainerBlock.Form might return value of _formBusinessService.BuildFormModel(this), but we have not assured that it does not sacrifice too much performance.

#171878
Nov 21, 2016 8:43
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.