Try our conversational search powered by Generative AI!

MVC XForm block element



I need to create XForm block element that can be added on any page that has block area. We are using ASP.NET MVC 4.

The first problem is that the XForm action url is for the block controller, e.g. http://localhost/XFormBlock/XFormPost?.. Well that's easy to fix with a custom routepath.

The second problem is that XFormPageHelper.GetXFormData-method assumes that xform is posted for a PageController, and fails when page is not found.

public class XFormPageHelper
    public XFormData GetXFormData(Controller controller, XFormPostedData xFormPostedData)
        XFormData data = XForm.CreateInstance(System.Guid.Parse(controller.Request.Params.get_Item("XFormId"))).CreateFormData();
        data.PageGuid = controller.HttpContext.GetService<PageRouteHelper>(null).Page.PageGuid;
        data.UserName = controller.User.Identity.get_Name();
        foreach (XFormsFragment fragment in from fragment in xFormPostedData.XFormFragments
            where !string.IsNullOrEmpty(fragment.Reference)
            select fragment)
            data.SetValue(fragment.Reference, fragment.Value);
        return data;


I guess it might be possible to make this work by adding contentId-parameters to action link, but it would be ugly as hell. There's got to be more beatiful solution for this, or is there?

Jan 07, 2013 14:09


I havn't tried this, but I have some tips what you can test to do.

1. Create a class that inherits from XFormPageUnknownActionHandler and register the class in the service locator (For<XFormPageUnknownActionHandler>().Use<MyClass>()).

2. Override ProcessXFormAction and replace the xFormHelper.GetXFormData with youre own code.

I know this is not a whole solution, but maby a startpoint. We do not have XForm support out of the box for Blocks in MVC.

Jan 09, 2013 8:54

To render xForm you should use @Html.DisplayForModel(new { XFormParameters = new XFormParameters { PostAction = "Post"} })

It will trigger your custom post action in the controller, and it will be before the data is posted.

In the controller you can do something like this:

public virtual ActionResult Post(SomePageType currentPage, XFormPostedData xFormPostedData)

     var xFormData = new XFormPageHelper().GetXFormData(this, xFormPostedData); //get xFormData that will be posted

     xFormData.SetValue(set some values); //changing some values in the data to be posted

         XFormActionHelper.DoAction(xFormData, xFormPostedData, true); //post data
         TempData["success"] = "Form submitted";
     catch (Exception exception)
         ModelState.AddModelError("ProcessXFormAction", exception.Message);

     return View("Index", model);

Hope this helps..


Jan 09, 2013 10:36
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* 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.