Solved

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

Posted on 2015-01-08
1
420 Views
Last Modified: 2015-01-16
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?
0
Comment
Question by:flynny
1 Comment
 
LVL 22

Accepted Solution

by:
ambience earned 500 total points
ID: 40539788
Shouldnt it be

Ajax.BeginForm("SendMail", "Home"
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now