Copied to clipboard

Flag this post as spam?

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


  • Ishan Vyas 67 posts 198 karma points
    Sep 24, 2018 @ 12:10
    Ishan Vyas
    0

    I am creating contact us form using mvc but whenever I click on send message button I get the partial view of error page only the success partial view never called In the loop? kindly help me with it the code is below. contact form below code @inherits UmbracoViewPage

    @using ELL.Web.Models

    @using (Ajax.BeginForm("SubmitForm", "Contact", new AjaxOptions() { UpdateTargetId = "form-result", HttpMethod = "POST", InsertionMode = InsertionMode.Replace, OnSuccess = "contactForm.showResult", OnFailure = "contactForm.showResult" }, new { id = "contact-form" })) {
    @Html.TextBoxFor(m => m.Name, new { placeholder = "Name" })
    @Html.TextBoxFor(m => m.Email, new { placeholder = "Email" })
    @Html.TextAreaFor(m => m.Message, new { placeholder = "Message", rows = "4" })
    }

    this is contact controller

    using System.Web.Mvc;
    

    using Umbraco.Web.Mvc; using ELL.Web.Models; using System.Net.Mail; using log4net; using System.Reflection;

    namespace ELL.Web.Controllers { public class ContactController : SurfaceController {

        public string GetViewPath(string name)
        {
    
            return $"/Views/Partials/Contact/{name}.cshtml";
        }
    
    
        [HttpGet]
        public ActionResult RenderForm()
        {
            ContactViewModel model = new ContactViewModel();
            return PartialView(GetViewPath("_ContactForm"), model);
        }
    
        [HttpPost]
        public ActionResult RenderForm(ContactViewModel model)
        {
    
            return PartialView(GetViewPath("_ContactForm"), model);
        }
    
    
        [HttpPost]
        public ActionResult SubmitForm(ContactViewModel model)
        {
            bool success = false;
            if (ModelState.IsValid)
            {
                success = SendEmail(model);
            }
            return PartialView(GetViewPath(success ? "_Success" : "_Error"));
        }
    
        public bool SendEmail(ContactViewModel model)
        {
            ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            try
            {
                MailMessage message = new MailMessage();
                SmtpClient client = new SmtpClient();
    
                string toAddress = System.Web.Configuration.WebConfigurationManager.AppSettings["ContactEmailTo"];
                string fromAddress = System.Web.Configuration.WebConfigurationManager.AppSettings["ContactEmailFrom"];
                message.Subject = $"Enquiry from: {model.Name} - {model.Email}";
                message.Body = model.Message;
                message.To.Add(new MailAddress(toAddress, toAddress));
                message.From = new MailAddress(fromAddress, fromAddress);
    
                client.Send(message);
                return true;
    
            }
            catch (System.Exception ex)
            {
                Log.Error("Contact Form Error", ex);
                return false;
            }
        }
    }
    

    }

    this is java script for contactform

    var contactForm = contactForm || {
    init: function () {
    
        this.listeners();
    },
    listeners: function () {
        $(document).on('click', '.contact-submit', function (e) {
    
            e.preventDefault();
            var form = $('#contact-form');
            form.submit();
        })
    
    },
    showResult: function () {
    
        $("#form-outer").hide();
        $("#form-result").show();
    }
    

    };

    contactForm.init();

    in contact controller the statement return PartialView(GetViewPath(success ? "Success" : "Error")); here the else part is only running it does not return for the success partial view

  • Paul Seal 524 posts 2889 karma points MVP 6x c-trib
    Sep 24, 2018 @ 21:45
    Paul Seal
    1

    Hi Ishan That code looks familiar. Have you tried stepping through the code using debugging?

    It would help to know where the code is failing.

    My first thought is that it is your SMTP settings in the web.config file. You can use a tool like SMTP4dev which acts like a mailserver on your local machine and lets you see what the email looks like which would have been sent.

    You could instead sign up to something like sendgrid.net and use those smtp settings instead and actually send the email.

    I hope this helps.

    Kind regards

    Paul

  • Ishan Vyas 67 posts 198 karma points
    Sep 25, 2018 @ 04:33
    Ishan Vyas
    0

    sir a added that code in web config file which you updated at the end:- this one

    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    
    <add key="ContactEmailTo" value="[email protected]" />
    <add key="ContactEmailFrom" value="[email protected]" />
    

    and also the one in mail settings also :-

     <system.net>
    <mailSettings>
      <smtp from="[email protected]">
        <network host="127.0.0.1" userName="username" password="password" port="25" />
      </smtp>
    </mailSettings>
    

    I cant get what is wrong in the code it seems like in controller there is some issues and in specifically the if else part when it returns the partial view if result is success or fail, in below line of code :-

     return PartialView(GetViewPath(success ? "_Success" : "_Error"));
    

    it happens to be like it does not go to the if part and it returns the else part which is error partial view. plz sir guide me what can i do in this.

  • Ishan Vyas 67 posts 198 karma points
    Sep 25, 2018 @ 06:07
    Ishan Vyas
    0

    thanks sir I got to know the problem, there was problem in the configuration of my smtp, as I work as a trainee I don't have that setup in the system, so the mail was not being send through the smtp. Thanks sir your video tutorials are great and they are a savior for me as there are not many tutorials on Umbraco , and you are explaining in deep about every single details to lookup on. we are waiting for another tutorials on it to learn more on Umbraco, and be a pro Umbracian!!!!

Please Sign in or register to post replies

Write your reply to:

Draft