Solved

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

Posted on 2015-01-08
1
410 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

9 Experts available now in Live!

Get 1:1 Help Now