Try our conversational search powered by Generative AI!

Forms javascript resources for all pages?


We are rendering form as a partial view in some cases. This means the form scripts are not rendered in such cases because they are not part of that view.
Is there a way to force form scripts to be included on all the pages?

We are using Forms 4.0.1

Edited, Apr 19, 2018 8:22

In your share layout view, you need to have @Html.RequiredClientResources("Header") and @Html.RequiredClientResources("Footer"), the forms script will be rendered. Is that solving your problem?

Apr 19, 2018 9:11

No it does not.
These resuorces are included if form is on the page. In our case page contains no form, so client resuorces contains no forms javascripts. Which make sense.

But in certain cases we add form as partial view through ajax call. However such form doesnt work, becauise resuorces are not addded. So I would like to have those resuorces added to all the pages, even without forms.

Or maybe there is a bertter way to do this?


Apr 19, 2018 9:15

How do you render form as partial view through ajax call? Create a page contains nothing except form, then using ajax call to render that page. Does it work?

Apr 19, 2018 9:22

What you are proposing would work, however that is not how partial views supposed to work. Rendering whole page would create complete html structutre which cannot be properly added to the existing page.

We essentialy render them as plain mvc partial views and result is some partial html markup, which is then included in the page with javascript. However, partial markup for the form doesnt have all the required scripts. 
I need to have those scripts  somehow in the page in advance if that would work, or maybe there is a way to render form as self sufficient component with all the required scripts?

Apr 19, 2018 9:33

I tried to add form resources like this:

    var fcbk = new EPiServer.Forms.Controllers.FormContainerBlockController();

This makes rtesuorces appear on the  page without amny form.

However, this seems to be not enough to make ajax form work. It seems that some things happen during page initialization and so such late added form will never work.
Can you confirm this?

Edited, Apr 19, 2018 13:35

There is a script which generate dynamically, the link look like: http://localhost:55918/EPiServer.Forms/DataSubmit/GetFormInitScript?formGuid=41e84e80-75cf-40ec-9a4c-ae83c69b628c&formLanguage=en

Please make sure you are running that script when you render your form (it should be not the empty form you created by C# code). And AFTER render forms using ajax, you need to run the EPiServerForms.js which contains some setup for DOM. Here is how we get the js resource and register it at the footer:

var resourcesFormsJs = EPiServerFrameworkSection.Instance.ClientResources.Debug ?
    "EPiServer.Forms.ClientResources.ViewMode.EPiServerForms.js" : "EPiServer.Forms.ClientResources.ViewMode.EPiServerForms.min.js";
    ModuleHelper.GetWebResourceUrl(typeof(FormContainerBlockController), resourcesFormsJs), "EPiServerForms.js",
    new List<string> { "Forms.jquery.js", "EPiServerForms_prerequisite.js" }).AtFooter();

Hope this help!

Apr 20, 2018 4:39

This might work for the first form, but there is a check to prevent multiple initialisation

if (epi.EPiServer.Forms.__Initialized) {

So that means it wont work if some other form exists on the page already.

Probably using iframes is the only feasible solution

Edited, Apr 20, 2018 8:01
* 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.