Copied to clipboard

Flag this post as spam?

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


  • Craig Richards 17 posts 75 karma points
    Feb 11, 2016 @ 11:57
    Craig Richards
    0

    7.3.7 AJAX contact form surface controller new page

    Hi Everyone,

    I've got a fairly straight forward AJAX contact form, which uses basically the same as my other sites, but using 7.3.7, the response is being posted back to a new page: /umbraco/Surface/Contact/SendEnquiry rather than the form

    Here's my view:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IceSigns.Models.Contact>
    

    @using IceSigns.Models;

    @{ HtmlHelper.ClientValidationEnabled = true; HtmlHelper.UnobtrusiveJavaScriptEnabled = true; var Node = Umbraco.Content(1060); }

    @using (Ajax.BeginForm("SendEnquiry", "Contact", new AjaxOptions

    { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "target", OnFailure = "ShowError()", OnSuccess = "ShowSuccess()" })) {

    @Html.TextBoxFor(model => model.Name, new Dictionary
    @Html.TextBoxFor(model => model.Email, new Dictionary
    @Html.TextBoxFor(model => model.Telephone, new Dictionary
    @Html.TextBoxFor(model => model.Company, new Dictionary
    @Html.TextAreaFor(model => model.Message, new Dictionary
    @Html.TextBoxFor(model => model.IsHuman, new Dictionary
    @if (Node.HasProperty("siteAddress")) { @Html.Raw(Node.GetPropertyValue("siteAddress").ToString()) }

    email: @Node.GetPropertyValue("siteEmail").ToString()
    tel: @Node.GetPropertyValue("siteTelephoneNumber").ToString()
    fax: @Node.GetPropertyValue("siteFaxNumber").ToString()

    }

    <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");
    }
    

    And my Controller:

    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; using ActionMailer.Net.Mvc; using IceSigns.Models;

    namespace IceSigns.Controllers { public class ContactController : Umbraco.Web.Mvc.SurfaceController {

        [HttpPost]
        [NotChildAction]
        public ActionResult SendEnquiry(Contact form)
        {
            //umbraco.NodeFactory.Node contactNode = new umbraco.NodeFactory.Node(1049);
    
            //string companyEmail = Convert.ToString(contactNode.GetProperty("contactFormEmail"));
    
            string Honeybear = form.Honeybear;
            string IsHuman = form.IsHuman;
    
    
    
            string retValue = "<p class='intro'>There was an error submitting the form, please try again later.</p>";
            if (!ModelState.IsValid)
            {
                return Content(retValue);
            }
    
            if (ModelState.IsValid)
            {
                if (!String.IsNullOrEmpty(Honeybear))
                {
                    retValue = "<p class='intro'>Don't fill in the honeybear!</p>";
                    return Content(retValue);
    
                }
                else
                {
                    retValue = "<p class='intro'><strong>Thank you!</strong> - your enquiry was submitted successfully. We will try to respond to your enquiry at the earliest opportunity.</p>";
    
                    new MailController().SendContactForm(form.Message, form.Email, form.Name, form.Telephone, form.Company).Deliver();
                    return Content(retValue);
                }
    
            }
    
            return Content(retValue);
        }
    }
    

    }

    Oh, and I've added to my web.config as normal.

    The emails are being sent, but as said, the response is being sent to a new page, rather than the form.

    edit: Not sure what's happened to the formatting?

  • Kasper Holm 47 posts 180 karma points
    Feb 18, 2016 @ 08:14
    Kasper Holm
    1

    Hello Craig

    Not sure if i understand the question totally.

    but du you have

     <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    

    In your web.config i've had some problems with ajax post if they are not there :)

  • Alex Lindgren 159 posts 356 karma points
    Feb 19, 2016 @ 15:14
    Alex Lindgren
    0

    I had a similar problem. Besides adding the appsettings Kasper mentioned, I installed Microsoft.jQuery.Unobtrusive.Ajax nuget package and add a reference to the script in my template.

    <script type="text/javascript" src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
    
Please Sign in or register to post replies

Write your reply to:

Draft