Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Andy Vennells 19 posts 59 karma points
    May 12, 2016 @ 23:49
    Andy Vennells
    0

    Access FormViewModel from Surface Controller handler

    How do I access the values from the Umbraco FormViewModel from my custom handler in my surface controller? For instance, I create a new Surface Controller derived from UmbracoFormsController and then create a custom Handler as follows:

    public class TestSurfaceController : Umbraco.Forms.Web.Controllers.UmbracoFormsController
    {
        [HttpPost]
        [ValidateCaptcha]
        [ValidateInput(true)]
        public void TestHandler(FormViewModel model, bool captchaIsValid)
        {
            if (ModelState.IsValid)
            {
                var test = 1;
            }
        }
    

    }

    However, when this gets called from my Forms.cshtml partial view, the only values I can get from the FormViewModel are FormID and FormName (see attached screen shot)

    FormViewModel

    I can get more data on the OnFormHandled method, but would like to process data pre handling (see screenshot)

    OnFormHandled

  • Andy Vennells 19 posts 59 karma points
    May 17, 2016 @ 00:24
    Andy Vennells
    0

    anyone able to assist?

  • Ben McKean 272 posts 549 karma points
    Oct 12, 2016 @ 14:07
    Ben McKean
    0

    Did you ever solve this?

  • Andrew Vennells 16 posts 101 karma points
    Oct 13, 2016 @ 03:55
    Andrew Vennells
    2

    Yes I did. In the Umbraco.Forms.Web assemly, you can get the answer there.

    I first had to create my own I extracted the GetForm method, and then used model.Build(form), so like this:

    public class TestSurfaceController : Umbraco.Forms.Web.Controllers.UmbracoFormsController
    {
       private Form GetForm(Guid formId)
       {
                using (FormStorage formStorage = new FormStorage())
                    return formStorage.GetForm(formId);
       }
    
        [HttpPost]
        [ValidateCaptcha]
        [ValidateInput(true)]
        public void TestHandler(FormViewModel model, bool captchaIsValid)
        {
            var form = this.GetForm(model.FormId);
            model.Build(form);
    
            //this will now have a built form model
            if (ModelState.IsValid)
            {
                var test = 1;
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft