Question

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?

Thanks.

Was it helpful?

Solution

You need to inherit your Controller from Umbraco.Web.Mvc.SurfaceController

You can do this in controller like this:

public class DefaultController : Umbraco.Web.Mvc.SurfaceController
{
   Public ActionResult YourMethod()
   {
       return CurrentUmbracoPage();
   }
}

In View you call the Form like:

@using (Html.BeginUmbracoForm("YourMethod", "Default",new { @class = "YourclassName" }))
{
    //DO Something
}

Now final step is to define your Route in RouteConfig.cs in App_Start Folder

 public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Default", action = "YourMethod", id = UrlParameter.Optional }
            );
        }
    }

OTHER TIPS

Try using this for your form:

@using (Html.BeginUmbracoForm("Post", "ControllerNameWithoutController", System.Web.Mvc.FormMethod.Post, new { @class = "YourclassName" }))

Also your controller must be inherited from SurfaceController

 public class YourControllerNameController : SurfaceController

I believe this is a good solution to allow get and post verbs to the same controller. As you cant inherit from both the RenderMvcController and SurfaceController then I have found this better, it also allows the use of the RenderModel for populating form elements.

 public class ContactUsController : SurfaceController, IRenderController
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top