Link to home
Start Free TrialLog in
Avatar of flynny
flynnyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Sending Ajax Contact Form with MVC and Returing Success/Error Partial View

Hi All,

I have a simple contact form where I want to basically send an email and post back a partial view with a success or error message and post it to a bootstrap modal popup.

I have the following ContactModel.cs in m Models folder;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace web1.Models
{
    public class ContactModel
    {
        [Required(ErrorMessage = "Name is required")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Telephone details are required")]
        public string Telephone { get; set; }

        [Required(ErrorMessage = "Your enquiry is required")]
        public string Enquiry { get; set; }
    }
}

Open in new window


I then add the form to my Index.cshtml file (it is appearing at the footer of the main page);

@model web1.Models.ContactModel

Open in new window


is at the top of the page and the form is as follows;

 @using (Ajax.BeginForm("SendMail", "ContactModel", new AjaxOptions { 
                                    HttpMethod = "POST",
                                    InsertionMode = InsertionMode.Replace, 
                                    UpdateTargetId = "modal" }))
                        {
                            @Html.ValidationSummary(true)
                            <div class="row">
                                <div class="col-md-12 form-group">
                                    @Html.ValidationMessageFor(model => model.Name)
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12 form-group">
                                    @Html.TextBoxFor(model => model.Name, new { @class = "form-control", @placeholder = "Your Name..." })
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12 form-group">
                                    @Html.ValidationMessageFor(model => model.Telephone)
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12 form-group">
                                    @Html.TextBoxFor(model => model.Telephone, new { @class = "form-control", @placeholder = "Your Contact Number..." })
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-12 form-group">
                                    @Html.ValidationMessageFor(model => model.Enquiry)
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12 form-group">
                                    @Html.TextAreaFor(model => model.Enquiry, new { @class = "form-control", @placeholder = "Any Additional Information or Message..." })
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <input type="submit" value="Apply Now!" class="btn btn-primary btn-lg" />
                                </div>
                            </div>
                        }

Open in new window


Finally in my HomeController.cs I have the method

[HttpPost]
        public ActionResult SendMail(ContactModel c)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    MailMessage msg = new MailMessage();
                    SmtpClient smtp = new SmtpClient();
                    MailAddress from = new MailAddress("xxx");
                    StringBuilder sb = new StringBuilder();
                    msg.To.Add("xxx");
                    msg.From = from;
                    msg.Subject = "Contact Form From Website";
                    msg.IsBodyHtml = false;
                    smtp.Host = "auth.smtp.1and1.co.uk";
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new System.Net.NetworkCredential("xxx", "xxx");
                    smtp.Port = 25;
                    smtp.EnableSsl = true;
                    sb.Append("Name: " + c.Name);
                    sb.Append(Environment.NewLine);
                    sb.Append("Telephone: " + c.Telephone);
                    sb.Append(Environment.NewLine);
                    sb.Append("Enquiry: " + c.Enquiry);
                    msg.Body = sb.ToString();
                    smtp.Send(msg);
                    msg.Dispose();
                    return PartialView("Success");
                }
                catch (Exception ex)
                {
                    return PartialView("Error");
                }
            }
            return PartialView();
        }

Open in new window


however when I click the method is not firing? any ideas what I am doing wrong here?
ASKER CERTIFIED SOLUTION
Avatar of ambience
ambience
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial