So Su Team : Web Development Tags : Web Development Issues Umbraco MVC

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

So Su Team : Web Development Tags : Web Development Issues Umbraco MVC

I'm upgrading a site to use Umbraco 7, and I thought I'd move some of the logic-in-the-view code into controllers. So I've got a fairly standard setup. I converted the Contact Us form first and was running into an issue that resulted i the YSOD. I was getting the error: Can only use UmbracoPageResult in the context of an Http POST when using a SurfaceController form.

Lets see some code:

My controller:

public class ContactUsController : SurfaceController
{
  [ChildActionOnly]
  public ActionResult ContactForm(int nodeId)
  {
    return PartialView(new ContactUsViewModel());
  }

  [HttpPost]
  public ActionResult ContactForm(ContactUsViewModel model)
  {
    if (ModelState.IsValid)
    {
      return RedirectToUmbracoPage(model.RedirectPage);
    }
    return CurrentUmbracoPage();
  }
}

Rendering the action is done like this in the document type's template:

<section id="contactForm" class="clearfix">
 @{ Html.RenderAction("ContactForm", "ContactUs", new {nodeId = (contentModel.Id)}); }
</section>

Debugging the issue, I was seeing that my controller's POST Action was getting called twice. So here's the rundown on what I was seeing.

  1. POST comes back to sever and the HttpPost method is called.
  2. ModelState is invalid so it returns the CurrentUmbracoPage
  3. Template renders the action "ContactForm" on the "ContactUs" controller
  4. HttpPost method is called a 2nd time! (WHAT?!)
  5. ModelState is invalid, all values in "model" are null, so it returns the CurrentUmbracoPage.
  6. Exception:  Can only use UmbracoPageResult in the context of an Http POST when using a SurfaceController form

Thanks to a second pair of eyes, we found that the 2nd call of the [HttpPost]ContactForm method was actually meant to be the call to the [HttpGet]ContactForm method. So because of naming, Umbraco confused itself and called the wrong method. Simple enough fix, change the name of the [HttpPost]ContactForm to [HttpPost]ContactSubmit and tada! fixed.

Oh, and don't forget to point the Html.BeginUmbracoForm call to the new method name [HttpPost]ContactSubmit.