Copied to clipboard

Flag this post as spam?

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


  • webico 1 post 71 karma points
    May 09, 2017 @ 03:30
    webico
    0

    How to fix No route error when using SurfaceController in Umbraco 7.1.8?

    I am trying to figure out how to get a simple Contact Form SurfaceController working. The problem is that when I try to navigate to the Contact Us page, I get a "No route in the route table matches the supplied values." error.

    My surface controller:

    namespace UmbracoSurface2.Controllers
    {
        public class ContactFormSurfaceController : SurfaceController
        {
            public ActionResult Index()
            {
                return PartialView("ContactForm", new ContactFormViewModel());
            }
        }
    }
    

    My ContactForm partial:

    @model UmbracoSurface2.Models.ContactFormViewModel
    
    <form>
        <div class="controls controls-row">
            @Html.TextBoxFor(m => m.Name)
            @Html.TextBoxFor(m => m.Email)
        </div>
        <div class="controls">
            @Html.TextAreaFor(m => m.Message, new { rows="5" })
        </div>
        <div class="controls">
            <button id="contact-submit" type="submit" class="btn btn-primary input-medium pull-right">Submit</button>
        </div>
    </form>    
    

    and my template:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = "Master.cshtml";
    }
    @Html.Action("Index", "ContactFormController")
    

    The one big difference between mine and the screencasts and examples I've found is that I'm using Visual Studio 2013 so the ASP.NET Website template has a newer version of MVC which I had to remove to get the UmbracoCms NuGet package to install. I also tried it without adding MVC to the project at the beginning.

    Any help/suggestions would be appreciated,

  • Dave Woestenborghs 3325 posts 11170 karma points MVP 5x admin c-trib
    May 09, 2017 @ 10:26
    Dave Woestenborghs
    0

    hi webico,

    Can you remove this line from your template :

    @Html.Action("Index", "ContactFormController")
    

    That way we can determine if it's related to the surface controller or another problem.

    Dave

  • Marc Goodson 1451 posts 9716 karma points MVP 5x c-trib
    May 09, 2017 @ 12:30
    Marc Goodson
    1

    Hi Webico

    I think your Surface Controller is called ContactFormSurfaceController

    and the convention is to remove the word 'Controller' from the name and so to render as an action you would use:

    @Html.Action("Index", "ContactFormSurface")
    

    otherwise you are saying look for a route on a ContactFormControllerController which doesn't exist hence the route missing error...

    If you want to handle the postback of the form the trick is to have the form elements wrapped in an Html.BeginUmbracoForm

    @using (Html.BeginUmbracoForm("HandlePost", "ContactFormSurface"))
    {
       <div class="controls controls-row">
            @Html.TextBoxFor(m => m.Name)
            @Html.TextBoxFor(m => m.Email)
        </div>
    
    etc
    }
    

    then have an action called HandlePost in your surface controller decorated with post:

    [HttpPost]
     public ActionResult HandlePost(YourFormViewModel model)
            {
                if (ModelState.IsValid == false)
                    return CurrentUmbracoPage();
    // else do stuff with posted values
       return RedirectToCurrentUmbracoPage();
    }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies