Copied to clipboard

Flag this post as spam?

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


  • Lachlann 344 posts 626 karma points
    Sep 14, 2014 @ 12:10
    Lachlann
    1

    Umbraco 7 Surfacce Controller Ajax.BeginForm

    Hello all,

    I working on a really really simple Umbraco 7 website which i have created on Azure. The main backend functionality I need to get working is a simple Contact form. I wasnt planning on setting up a big VS studio project for such a small website. Initially I thought there might be a simple contact form project that would work on U7 but I couldnt see any projects thta were confirmed to work. So I tried to implement my own simple Ajaxy form based on this tutorial: 

    http://www.systenics.com/blog/creating-an-ajax-enabled-contact-form-in-umbraco-6-with-aspnet-mvc-4-and-twitter-bootstrap/?tag=Umbraco+6

    The difference is that becuase i have not setup a VS project i didnt put my model or surface controller into the model/controller folders (cause the dont exist in the deploy) I put my code staright into the App_Code folder.

    After some errors with references etc... I manage to get a working page. However when i submit my ajax form the surface controller seems to just return the page markup (the page doesnt refresh i just get the page html posted back)

    Here is my code:

    Model:

    using System;
    using System.ComponentModel.DataAnnotations;
    using System;
    using System.ComponentModel.DataAnnotations;

    public class ContactViewModel
    {
    [Required]
    public string Name { get; set; }

    [Required]
    public string Email { get; set; }

    [Required]
    [Display(Name = "Message:")]
    public string Message { get; set; }
    }

     

    Controller (simplified it doesnt actually do anything yet):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Web.Mvc;
    using System.Net.Mail;
    using System.Net;


    public class ContactFormSurfaceController : Umbraco.Web.Mvc.SurfaceController
    {
    [HttpPost]
    [NotChildAction]
    public ActionResult SendMail(ContactViewModel form)
    {
    string retValue = "There was an error submitting the form, please try again later.";
    if (!ModelState.IsValid)
    {
    return Content(retValue);
    }

    if (ModelState.IsValid)
    {

    retValue = "Hey";
    }
    return Content(retValue);
    }
    }

     

    Partial Veiw:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<ContactViewModel>
    <div id="status"></div>

    @using (Ajax.BeginForm("SendMail", "ContactFormSurface", new AjaxOptions { HttpMethod = "POST",
    InsertionMode = InsertionMode.Replace, UpdateTargetId = "target",
    OnFailure = "ShowError()", OnSuccess = "ShowSuccess()" }))
    {

    <fieldset>
    <legend>Fill the form below and we will contact you soon</legend>

    @Html.LabelFor(model => model.Name)
    @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name)

    @Html.LabelFor(model => model.Email)
    @Html.EditorFor(model => model.Email) @Html.ValidationMessageFor(model => model.Email)

    @Html.LabelFor(model => model.Message)
    @Html.TextAreaFor(model => model.Message) @Html.ValidationMessageFor(model => model.Message)<br />

    <input type="submit" class="btn btn-primary" value="Send Message">
    </fieldset>
    }

    <script>
    function ShowError() {
    $("#status").removeClass();
    $("#status").addClass("alert alert-error");
    $("#status").html("<strong>Error!</strong> There was an error posting the contact form. Please try again later.");
    }

    function ShowSuccess() {
    $("#target").removeClass();
    $("#target").addClass("alert alert-success");
    }
    </script>

     

    View (jsut the line that called the partial):

    @Html.Partial("ContactForm", new ContactViewModel())

     

    The code above actually givesa 404 error when submitted it says:


    HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. &nbsp;Please review the following URL and make sure that it is spelled correctly.

    /umbraco/Surface/ContactFormSurface/SendMail

    If i try and changes any of the names deifned in the Ajax call in the partial i get no 404 but the form just posts back the current page.

    I guess i have 2 questions:

    1. Can i call a surface controller that is in the App_Code folder? if so what am I doing wrong?
    2.  If this wont work are there any simple contact form projects I may have missed that I can use instead?

    Thanks

    Lachlann

     

     

  • Lachlann 344 posts 626 karma points
    Sep 14, 2014 @ 12:48
    Lachlann
    0

    Update here is a bit more infomration from the error:

    [HttpException]: The controller for path &#39;/umbraco/Surface/ContactForm/SendMail&#39; was not found or does not implement IController.  at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
  • Lachlann 344 posts 626 karma points
    Sep 14, 2014 @ 13:07
    Lachlann
    0

    This was answered by @Cultiv (Sebastiaan Janssen)

    The problem was that I needed to have a namespace defined as the code was executing from the App_Code folder. As soon as I did this my code executed.

    Thansk Sebastiaan!

Please Sign in or register to post replies

Write your reply to:

Draft