MVC Feedback form with email notification

MVC Feedback form with email notification also save data into database.

To make it short only code is there no description added  if any doubt feel free to contact me.

Feedback.cshtml

@model MyWeb.Models.Feedback

@{
ViewBag.Title = “Feedback”;
}

<h2>Feedback</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true, “Please correct the errors and try again.”)
<div>
<fieldset>
<legend>Feedback Form</legend>
<div>
@Html.LabelFor(m => m.FirstName)
</div>
<div>
@Html.TextBoxFor(m => m.FirstName)
@Html.ValidationMessageFor(m => m.FirstName)
</div>

<div>
@Html.LabelFor(m => m.LastName)
</div>
<div>
@Html.TextBoxFor(m => m.LastName)
@Html.ValidationMessageFor(m => m.LastName)
</div>

<div>
@Html.LabelFor(m => m.EmailAddress)
</div>
<div>
@Html.TextBoxFor(m => m.EmailAddress)
@Html.ValidationMessageFor(m => m.EmailAddress)
</div>

<div>
@Html.LabelFor(m => m.Telephone)
</div>
<div>
@Html.TextBoxFor(m => m.Telephone)
@Html.ValidationMessageFor(m => m.Telephone)
</div>

<div>
@Html.LabelFor(m => m.Company)
</div>
<div>
@Html.TextBoxFor(m => m.Company)
@Html.ValidationMessageFor(m => m.Company)
</div>

<div>
@Html.LabelFor(m => m.Address1)
</div>
<div>
@Html.TextBoxFor(m => m.Address1)
@Html.ValidationMessageFor(m => m.Address1)
</div>
<div>
@Html.LabelFor(m => m.Address2)
</div>
<div>
@Html.TextBoxFor(m => m.Address2)
@Html.ValidationMessageFor(m => m.Address2)
</div>
<div>
@Html.LabelFor(m => m.City)
</div>
<div>
@Html.TextBoxFor(m => m.City)
@Html.ValidationMessageFor(m => m.City)
</div>
<div>
@Html.LabelFor(m => m.State)
</div>
<div>
@Html.TextBoxFor(m => m.State)
@Html.ValidationMessageFor(m => m.State)
</div>
<div>
@Html.LabelFor(m => m.Zip)
</div>
<div>
@Html.TextBoxFor(m => m.Zip)
@Html.ValidationMessageFor(m => m.Zip)
</div>
<div>
@Html.LabelFor(m => m.Description)
</div>
<div>
@Html.TextAreaFor(m => m.Description)
@Html.ValidationMessageFor(m => m.Description)
</div>
<p>
<input type=”submit” value=”Submit” />
</p>
</fieldset>
</div>
}

Inside Models folder in Feedback.cs

Auto-generated code using Entity Framework 4 refer previous articles

public partial class Feedback
{
#region Primitive Properties

public virtual int Id
{
get;
set;
}

[Required]
[DisplayName(“First name”)]
public virtual string FirstName
{
get;
set;
}

[Required]
[DisplayName(“Last name”)]
public virtual string LastName
{
get;
set;
}

[RegularExpression(@”^([a-zA-Z0-9_-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([a-zA-Z0-9-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$”, ErrorMessage = “Please enter a valid e-mail adress”)]
[Required]
[DisplayName(“Email Address”)]
public virtual string EmailAddress
{
get;
set;
}

[RegularExpression(@”^d+$”, ErrorMessage = “Please enter proper contact details.”)]
public virtual Nullable<int> Telephone
{
get;
set;
}

public virtual string Company
{
get;
set;
}

public virtual string Address1
{
get;
set;
}

public virtual string Address2
{
get;
set;
}

public virtual string City
{
get;
set;
}

public virtual string State
{
get;
set;
}

[RegularExpression(@”^d+$”, ErrorMessage = “Please enter proper zip details.”)]
public virtual Nullable<int> Zip
{
get;
set;
}

public virtual string Description
{
get;
set;
}

#endregion
//send mail to admin. code is not auto generated

public void SendEnquiryEmail(Feedback enquiry)
{
StringBuilder mailBody = new StringBuilder();
mailBody.AppendFormat(“<h2>Feedback details</h2>”);
mailBody.AppendFormat(“<br />”);
mailBody.AppendFormat(“<p>First Name : ” + enquiry.FirstName + “</p>”);
mailBody.AppendFormat(“<p>Last Name : ” + enquiry.LastName + “</p>”);
mailBody.AppendFormat(“<p>Email : ” + enquiry.EmailAddress + “</p>”);
mailBody.AppendFormat(“<p>Telephone : ” + enquiry.Telephone + “</p>”);
mailBody.AppendFormat(“<p>Company : ” + enquiry.Company + “</p>”);
mailBody.AppendFormat(“<p>Address1 : ” + enquiry.Address1 + “</p>”);
mailBody.AppendFormat(“<p>Address2 : ” + enquiry.Address2 + “</p>”);
mailBody.AppendFormat(“<p>City : ” + enquiry.City + “</p>”);
mailBody.AppendFormat(“<p>State : ” + enquiry.State + “</p>”);
mailBody.AppendFormat(“<p>Zip : ” + enquiry.Zip + “</p>”);
mailBody.AppendFormat(“<p>Description : ” + enquiry.Description + “</p>”);

var message = new MailMessage(ConfigurationManager.AppSettings[“From”], ConfigurationManager.AppSettings[“To”])
{
IsBodyHtml = true,
Subject = “Feedback details”,
Body = mailBody.ToString()

};
var client = new SmtpClient();
client.Send(message);
}
}

Controller code inside Controllers folder

FeedbackController.cs

public class FeedbackController : Controller
{
//need to use repository pattern

ASPNETDBEntities1 dbEntities = new ASPNETDBEntities1();

//
// GET: /Feedback/

public ActionResult Index()
{
return View();
}

//
// GET: /Feedback/

public ActionResult Enquiry()
{
return View();
}

//
//POST: /Feedback/ Save data into database and once done redirect to thank you  page.

[HttpPost]
public ActionResult Enquiry(Feedbackenquiry)
{
if (ModelState.IsValid)
{
dbEntities.Enquiries.AddObject(enquiry);
dbEntities.SaveChanges();
enquiry.SendEnquiryEmail(enquiry);

return RedirectToAction(“Thankyou”);
}
return View(enquiry);
}

//redirect to thankyou page
public ActionResult Thankyou()
{
return View();
}
}

//create simple thank you page inside View folder

web.config

<system.net>
<mailSettings>
<smtp deliveryMethod=”Network”>
<network host=”mail.gmail.com” userName=”username” password=”password” />
</smtp>
</mailSettings>
</system.net>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s