submitsitecorenullreferenceexceptionweb-forms-for-marketers

Save Action on webforms for marketers throws exception


I am using the latest version of Sitecore - 7.2 (rev. 140228) and the latest version of WFFM - 2.4 rev. 140923. So the problem is whenever I try to submit a form no matter what kind of save action I have, Sitecore throws exception:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   Sitecore.Forms.Mvc.Controllers.ModelBinders.FormModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +571
   System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +457
   System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +152
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +641

Some additional information. The latest version of WFFM supports MVC so in this scenario I use cshtml layout. This is the blog post where is described how to use MVC - THIS Despite it is for previous versions of SC it doesn't matter and the solution is applicable for the latest version.


Solution

  • Think I have a solution for this.

    I reverse engineered and debugged using a version of the controller and model binder I created after reflecting on the Sitecore.Forms.MVC classes using ILSpy.

    Apparently the null reference is occuring when it hits this:

    if (controllerContext.HttpContext.Request.RequestType == "POST" && formId != item.ID)
    

    item is null. item is set in the line before:

    Sitecore.Data.Items.Item item = RenderingContext.CurrentOrNull.PageContext.Database.GetItem(RenderingContext.CurrentOrNull.Rendering.DataSource);
    

    The data source property is empty on the rendering. If you check your presentation details, you'll see that's because there is another field that is used to specify your form ID. Apparently this looks like a Sitecore bug. But an easy workaround is to set the form data source in both the form ID and the Data source property of the rendering.