Skip to content

Intercepting requests with your own controller and form posts

Simon Yohannes edited this page Oct 20, 2019 · 9 revisions

puck relies on on a catch all route in the Startup.cs file. you can intercept requests by adding a new route before the puck catch all route or optionally you can use attribute routing on your controller.

let's say you have Register form at the path /members/register, you will need to make a controller that inherits from puck.core.Controllers.BaseController with a Register action:

public class RegisterController : BaseController
{
    [HttpPost]
    [Route("members/register")]
    public ActionResult Register(RegisterModel model) {
        if (ModelState.IsValid) {
            //handle post
            Response.Redirect("/users/register/success");
        }
        //if invalid, return current puck page
        ViewBag.RegisterModel = model;
        return base.Puck();
    }
}

as you can see, there is a Register action with a route attribute to handle the form post. if the post is successful, you might want to redirect to a success page and if the model is invalid, you might want to return the current page. as you can see, the RegisterModel is put in the ViewBag when returning the current puck page, and when in your view you can pass the RegisterModel to a partial which will handle displaying the form.

Get current ViewModel from your controller action

if you've intercepted a page with your controller action you can grab the page's ViewModel by doing this:

var currentNode = QueryHelper<Page>.Current();