Created
June 3, 2014 13:43
-
-
Save carlosmartinezt/6f3809a47e9a7a3c0e08 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ContactController : Umbraco.Web.Mvc.SurfaceController | |
{ | |
[ChildActionOnly] | |
public ActionResult ContactForm() | |
{ | |
var model = new ContactModel(); | |
//Initialize model however you want | |
model.FullName = "Enter your name"; | |
//In case you need to access the current node | |
var currentNode = Umbraco.TypedContent(UmbracoContext.PageId.GetValueOrDefault()); | |
//In case you need to access the home node | |
var home = currentNode.AncestorsOrSelf(0).First(); | |
return PartialView("ContactForm", model); | |
} | |
[HttpPost] | |
public ActionResult ContactForm(ContactModel model) | |
{ | |
// server validation here | |
// TempData["ErrorMessage"] = "Error processing field ..."; | |
if (ModelState.IsValid) | |
{ | |
var currentNode = Umbraco.TypedContent(UmbracoContext.PageId.GetValueOrDefault()); | |
var homeNode = currentNode.AncestorsOrSelf(0).First(); | |
var sendEmailsFrom = homeNode.GetPropertyValue<string>("sendEmailsFrom") ?? "[email protected]"; | |
var sendEmailsTo = homeNode.GetPropertyValue<string>("sendEmailsTo") ?? "[email protected]"; | |
var body = String.Format("From: {0}, Email: {1}, Tel: {2} and Message: {3}", model.FullName, model.Email, model.Telephone, model.Message); | |
var subject = "Travel Concierge - Message Sent"; | |
try | |
{ | |
umbraco.library.SendMail(sendEmailsFrom, sendEmailsTo, subject, body, true); | |
TempData["InfoMessage"] = "Your message has been successfully sent and we will be in touch soon..."; | |
// Clear all the form fields | |
ModelState.Clear(); | |
model.FullName = string.Empty; | |
model.Telephone = string.Empty; | |
model.Email = string.Empty; | |
model.Message = string.Empty; | |
//redirect to current page to clear the form | |
return RedirectToCurrentUmbracoPage(); | |
} | |
catch (Exception ex) | |
{ | |
TempData["ErrorMessage"] = ex.Message + ex.StackTrace; | |
} | |
} | |
return CurrentUmbracoPage(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment