Solved

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

Posted on 2015-01-08
1
464 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 22

Accepted Solution

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

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
This article discusses how to implement server side field validation and display customized error messages to the client.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

707 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