Copied to clipboard

Flag this post as spam?

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


  • Thomas Boeriis 3 posts 23 karma points
    Jul 19, 2013 @ 14:52
    Thomas Boeriis
    0

    Contact Form SurfaceController not return content the right way

    Hi everyone

    Quite new to Umbraco so followed a guide to set up an "Creating an Ajax enabled Contact Form in Umbraco 6 with ASP.NET MVC 4 and Twitter Bootstrap" (http://www.systenics.com/blog/creating-an-ajax-enabled-contact-form-in-umbraco-6-with-aspnet-mvc-4-and-twitter-bootstrap/?tag=Umbraco+6)

    Allmost got all working except when submit my form it dosen't return the view with the sucess or error text, insted it returns the text on a blank new page kinda lost in what to do, im getting the mail and all just not returning like in the guide.

    Controller

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Rebildpark.Models;
    using System.Net.Mail;
    using System.Net;
    
    namespace Rebildpark.Controllers
    {
        public class ContactController : Umbraco.Web.Mvc.SurfaceController
        {
            [HttpPost]
            public ActionResult SendMail(Contact form)
            {
                string retValue = "Der opstod en fejl, prøv igen senere eller kontakt via email istedet.";
                if (!ModelState.IsValid)
                {
                    return Content(retValue);
                }
    
                if (ModelState.IsValid)
                {
                    //Update your SMTP server credentials
                    using (var client = new SmtpClient
                    {
                        Host = "smtp.gmail.com",
                        Port = 587,
                        EnableSsl = true,
                        Credentials = new NetworkCredential("HIDDEN","HIDDEN"),
    DeliveryMethod = SmtpDeliveryMethod.Network }) { var mail = new MailMessage(); mail.To.Add("HIDDEN"); // Update your email address mail.From = new MailAddress(form.Email, form.Name); mail.Subject = String.Format("Rebil Park Kontakt Formel fra - {0}", form.Name); mail.Body = form.Message; mail.IsBodyHtml = false; try { client.Send(mail); retValue = "Din besked er blevet sendt, du vil få svar hurtigst muligt"; } catch (Exception e) { throw e; } } } return Content(retValue); } } }
    Partial view
    @inherits Umbraco.Web.Mvc.UmbracoViewPage<Rebildpark.Models.Contact>
    <div id="status"></div>
    @using (Ajax.BeginForm("SendMail", "Contact", new AjaxOptions { HttpMethod = "POST", 
        InsertionMode = InsertionMode.Replace, UpdateTargetId = "target", 
        OnFailure = "ShowError()", OnSuccess = "ShowSuccess()" }))
    {
    
    
            <h5>Send mail til os</h5>
            <div class="contactArea">
            @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, htmlAttributes: new { @class="contactText" }) @Html.ValidationMessageFor(model => model.Message)<br />
    
            <input type="submit" class="button largebtn normalfont orange" value="Send">
            </div>
    }
    
    <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>
  • Charles Afford 1163 posts 1709 karma points
    Jul 21, 2013 @ 17:53
    Charles Afford
    0
  • Charles Afford 1163 posts 1709 karma points
    Jul 21, 2013 @ 17:56
    Charles Afford
    0

    Is the razor logic you have posted in a partial view or a view?

    If the post is from a view you need to return a view if its from a partial then you need to return a partial.

    So:

    Return PartialView("partialname",model);

    Dont return Content (99% sure this is the problem)

    I would try an abstract the logic you have for you email functinoality in to a method that you can call from your controller and not have it in the controller its self.

  • Charles Afford 1163 posts 1709 karma points
    Jul 21, 2013 @ 18:02
    Charles Afford
    0

    If you need to get data from Umbraco doing this, you will need to Node and NodeID on  property because it will lose context.

    Best way i have found is to create a NodeID and Node as a property.  Set the NodeId in the view.  Then in Node property check if the Node is not null, if it is then get it with the nodeId if not then set the nodeid property using the Node.

    Hope this makes sense :)

    Charlie

  • Thomas Boeriis 3 posts 23 karma points
    Jul 22, 2013 @ 17:31
    Thomas Boeriis
    0

    Its a Partial view that i render like so:

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

    but found a new workaround not quite good but it works for this small project.

    didnt had to get data from umbraco so it will work just fine.

     

Please Sign in or register to post replies

Write your reply to:

Draft