Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2015-01-08
1
Medium Priority
?
535 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 2000 total points
ID: 40539788
Shouldnt it be

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

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

618 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