YouTube Video Tutorial

This post is to accompany the YouTube video tutorial I created. I will share the code I wrote for each section in the video.

1. Create the Contact template

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
    Layout = "Master.cshtml";
}

2. Create the Contact document type

3. Associate the Contact template with the Contact document type

4. Create a Model to receive the data

using System.ComponentModel.DataAnnotations;
namespace InstallUmbraco.Models
{
    public class ContactModel
    {
        [Required]
        [Display(Name ="First Name:")]
        public string FirstName { get; set; }

        [Required]
        [Display(Name = "Last Name:")]
        public string LastName { get; set; }

        [Required]
        [EmailAddress]
        [Display(Name = "Email Address:")]
        public string EmailAddress { get; set; }

        [Required]
        [Display(Name = "Message:")]
        public string Message { get; set; }
    }
}

5. Create a Controller with a method to Render the form

using Umbraco.Web.Mvc;
using System.Web.Mvc;
using InstallUmbraco.Models;
using System.Net.Mail;

namespace InstallUmbraco.Controllers
{
    public class ContactSurfaceController : SurfaceController
    {
        public const string PARTIAL_VIEW_FOLDER = "~/Views/Partials/Contact/";

        public ActionResult RenderForm()
        {
            return PartialView(PARTIAL_VIEW_FOLDER + "_Contact.cshtml");
        }
    }
}

6. Add a method to the controller to Submit the form and send an email

using Umbraco.Web.Mvc;
using System.Web.Mvc;
using InstallUmbraco.Models;
using System.Net.Mail;

namespace InstallUmbraco.Controllers
{
    public class ContactSurfaceController : SurfaceController
    {
        public const string PARTIAL_VIEW_FOLDER = "~/Views/Partials/Contact/";
        public ActionResult RenderForm()
        {
            return PartialView(PARTIAL_VIEW_FOLDER + "_Contact.cshtml");
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(ContactModel model)
        {
            if(ModelState.IsValid)
            {
                SendEmail(model);
                TempData["ContactSuccess"] = true;
                return RedirectToCurrentUmbracoPage();
            }
            return CurrentUmbracoPage();
        }

        private void SendEmail(ContactModel model)
        {
            MailMessage message = new MailMessage(model.EmailAddress, "[email protected]");
            message.Subject = string.Format("Enquiry from {0} {1} - {2}", model.FirstName, model.LastName, model.EmailAddress);
            message.Body = model.Message;
            SmtpClient client = new SmtpClient("127.0.0.1", 25);
            client.Send(message);
        }
    }
}

7. Create a partial view for the Contact form

@inherits UmbracoViewPage<InstallUmbraco.Models.ContactModel>
@using (Html.BeginUmbracoForm("SubmitForm", "ContactSurface", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    <div class="form-group">
        @Html.ValidationSummary()
    </div>
    <div class="form-group">
        <div class="col-xs-3">
            @Html.LabelFor(m => m.FirstName)
        </div>
        <div class="col-xs-9">
            @Html.TextBoxFor(m => m.FirstName)
        </div>
    </div>
    <div class="form-group">
        <div class="col-xs-3">
            @Html.LabelFor(m => m.LastName)
        </div>
        <div class="col-xs-9">
            @Html.TextBoxFor(m => m.LastName)
        </div>
    </div>
    <div class="form-group">
        <div class="col-xs-3">
            @Html.LabelFor(m => m.EmailAddress)
        </div>
        <div class="col-xs-9">
            @Html.TextBoxFor(m => m.EmailAddress)
        </div>
    </div>
    <div class="form-group">
        <div class="col-xs-3">
            @Html.LabelFor(m => m.Message)
        </div>
        <div class="col-xs-9">
            @Html.TextAreaFor(m => m.Message)
        </div>
    </div>
    <button>Submit</button>
}

8. Add the form to the Contact template

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
    Layout = "Master.cshtml";
}
<div class="container">
    @if (TempData["ContactSuccess"] != null && (bool)TempData["ContactSuccess"])
    {
        <p>Your message was sent successfully</p>
    }
    else
    {
        Html.RenderAction("RenderForm", "ContactSurface");
    }
</div>

9. Create a Contact page using the Contact document type

10. Demonstrate PaperCut for testing emails

https://papercut.codeplex.com/

Papercut allows you to you test emails without the need for a physical SMTP server.

11. Test it works

Paul Seal

Umbraco MVP and .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Proudly sponsored by

Moriyama

  • Moriyama build, support and deploy Umbraco, Azure and ASP.NET websites and applications.
AppVeyor

  • CI/CD service for Windows, Linux and macOS
  • Build, test, deploy your apps faster, on any platform.
stkrs

  • Custom stickers for startups, artists and brands.
  • Bespoke easy-apply, removable, custom brand stickers printed in the UK.
elmah.io

  • elmah.io is the easy error logging and uptime monitoring service for .NET.
  • Take back control of your errors with support for all .NET web and logging frameworks.
uSync Complete

  • uSync.Complete gives you all the uSync packages, allowing you to completely control how your Umbraco settings, content and media is stored, transferred and managed across all your Umbraco Installations.