MVC XForm block element

Jouni Huttunen
Member since: 2010
 

Hi!

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?

#64700 Jan 07, 2013 14:09
  •  

    Hello,


    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.

    #64759 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:

    [AcceptVerbs(HttpVerbs.Post)]
    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

         try
         {
             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..

    Phil

    #64761 Jan 09, 2013 10:36