Copied to clipboard

Flag this post as spam?

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


  • Daniel Jensen 29 posts 160 karma points
    Sep 11, 2019 @ 13:56
    Daniel Jensen
    0

    How to return view with a model from Surface controller

    Hi, I'm trying to post form data to a surface controller, and return a view saying something like "thanks for your form data entry {Name}".

    This is what my code is boiled down to: (It should illustrate what I wish to achieve)

    namespace Cookie.Web.Controllers
    {
      public class WizardSurfaceController : SurfaceController
      {
          [HttpPost]
          public ActionResult PassFormData()
          {
            var formData = Request.Form;
    
            var name = formData["name"];
            var address = formData["address"];
            var email = formData["email"];
    
            var wvm = new WizardViewModel()
            {
                Name = name,
                Address = address,
                Email = email
            };
    
            return View("~/Views/statusPage.cshtml", wvm);
        }
    }
    
      public class WizardViewModel
      {
          public string Name { get; set; }
          public string Address { get; set; }
          public string Email { get; set; }
      }
    }
    

    How do i pass data to my view? Its not a partial view, so it should also inherit the layout.

    If I try to run the snippet above, I just get this:

    Compiler Error Message: CS0115: 'PageViewsstatusPagecshtml.Execute()': no suitable method found to override

  • Shaishav Karnani from digitallymedia.com 354 posts 1638 karma points
    Sep 12, 2019 @ 05:36
    Shaishav Karnani from digitallymedia.com
    0

    Hi Daniel,

    You need to return CurrentUmbracoPage();

    This is good article that shows how to pass data back to the front-end. https://our.umbraco.com/forum/using-umbraco-and-getting-started/98900-multi-step-form-implementation

    Cheers,

    Shaishav

  • Daniel Jensen 29 posts 160 karma points
    Sep 12, 2019 @ 09:52
    Daniel Jensen
    0

    @Shaishav It didn't seem to work with using the CurrentUmbracoPage(); It throws this error:

    System.InvalidOperationException: Can only use UmbracoPageResult in the context of an Http POST when using a SurfaceController form

    But RedirectToUmbracoPage(id, querystring); did work. It's however not quite what I was looking for. It will probably work if I either pass the data in the query string, or store it in the db, and pull it out in the razor template. It just seems farfetched from best practice.

Please Sign in or register to post replies

Write your reply to:

Draft