8

I am trying to insert a form into a page via code; however, I can't get it to work.

I am using the code from this Sitecore article:

Insert a web form directly on a web page

@using Sitecore.Mvc.Presentation;

@{
    RenderingContext.Current.Rendering.Parameters["FormId"] = "<id of the form item>";
}

@Html.Sitecore().Controller("Sitecore.Forms.Mvc.Controllers.FormController, Sitecore.Forms.Mvc")

Where <id of the form item> is being replaced with the form's item ID.

The page tries to render the form using the data source of the current rendering instead of the <id of the form item>. Has anyone else experienced this or solved it?

  • Sitecore 8.1 rev. 160519
  • Web Forms for Marketers 8.1 rev. 160523
1
  • Does it work if you, instead of inserting it via code in the MVC view, insert it via the Content Editor like described in the same KB page? Oct 26, 2016 at 6:47

2 Answers 2

6

@Paul George is correct when he says there is a problem with the MVC Form Rendering and it requires a DataSource instead of a FormID Parameter.

Now this causes a big problem, because there is no way of setting DataSource to the @Html.Sitecore.Controller() Helper Method.

The only way to solve this is by actually rendering the form with @Html.Sitecore.Rendering() which support DataSource.

So the code should look like this

@Html.Sitecore().Rendering("{F2CCA16D-7524-4E99-8EE0-78FF6394A3B3}", new { Datasource = "<id of the form item>"})

Where {F2CCA16D-7524-4E99-8EE0-78FF6394A3B3} is the ID of the Mvc Form Controller rendering which is used for inserting forms in placeholders.

But after testing this there is actually an exception that there is no UniqueId set for the Form, so it throws out an exception that the UniqueId (which is required) is empty. So the actual code that should be used for rendering form in this manner is:

@Html.Sitecore().Rendering("{F2CCA16D-7524-4E99-8EE0-78FF6394A3B3}", new { Datasource = "<id of the form item>" , UniqueId = "<unique id of the form rendering>"})

I don't think there is problem setting <unique id of the form rendering> to some value you manually choose (or generate), but I might be wrong here. I tested a couple of submissions and they passed successfully.

Still as this functionality is obviously not very tested and probably unstable, I think that the best solution for you might be to just add the form the old fashioned way, by inserting it into a placeholder.

5

From memory there is a discrepancy with the use of the 'formId' parameter vs setting the datasource of the form rendering to the desired form.

I believe when using MVC you need set the datasource, not the rendering parameter. You can call the wffm rendering as a rendering rather than a controller, which should allow you to do this:

@Html.Sitecore().Rendering(wffmFormRenderingId, new { DataSource = formId })
2
  • Please include sample code that would fix the issue. Right now, your answer is not clear enough. Oct 26, 2016 at 10:05
  • Good point, updated answer. Oct 26, 2016 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.