Dimple
Dimple

Reputation: 33

POST data using RenderMvcController in Umbraco

I have set up a document type in Umbraco, and have created a custom controller & model for this document type.

The custom controller inherits from : RenderMvcController class and the views inherit the model through @inherits Umbraco.Web.Mvc.UmbracoViewPage<com.something.model>

This all works fine for any HttpGet requests. However as soon as I want to do a form post back to the controller using @using (Html.BeginUmbracoForm("SomeAction", "SomeController", null, new { @class = "some-class" }))

I get the following error message: Could not find a Surface controller route in the RouteTable for controller name SomeController

From all the documentation that I was able to find it always refers to SurfaceControllers when it comes to form posts. Is there a way to change the routing so that it would post to my custom controller, rather then another controller that inherits from the SurfaceController class?

Upvotes: 3

Views: 4934

Answers (2)

Digbyswift
Digbyswift

Reputation: 10400

If you are going to post the form in this way, you need two controllers. One for the Document Type that inherits from MvcRenderController (as you already have) and a second which inherits from the SurfaceController.

The surface controller just needs a single POST action that does one of the following things:

// e.g. if modelstate is invalid
return CurrentUmbracoPage();

// redirecting back to original page after a successful post
return RedirectToCurrentUmbracoPage(); 

// Redirecting to another page after a successful post
return RedirectToUmbracoPage(12345)

This has been taken from the documentation here: http://our.umbraco.org/documentation/Reference/Templating/Mvc/forms

Strictly speaking the initial document type controller is not necessary in this scenario as it does not play a part in the post of the form.

If you want to post directly to the custom controller then you should not use Html.BeginUmbracoForm, you should just post directly to the current URL. The complication here is that it is then a little tricky to bind your model as a parameter of the post action. Your view model will have to inherit from RenderModel in the following way:

public class BaseModel : RenderModel
{
    public BaseModel() :
      base(UmbracoContext.Current.PublishedContentRequest.PublishedContent) { }
}

Upvotes: 5

Philip Pittle
Philip Pittle

Reputation: 12295

According to http://our.umbraco.org/forum/developers/api-questions/38662-(v6)-Could-not-find-a-Surface-controller-route-error?p=0,

SomeController Needs to inherit from SurfaceController, not RenderMvcController.

Alternatively, you could create a dedicate route and point it directly to your controller:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
         routes.MapRoute("someController", "some/someAction",
            new { controller = "Some", action = "SomeAction" });
    }
}

Upvotes: 0

Related Questions