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

Want to thank me?

If I've helped you out and you want to thank me, why not buy me a coffee?

Buy me a coffee

About the author

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.

Related Posts

How to animate tree icons in Umbraco v8

This post shows you how you can change the colour and animate tree icons in Umbraco based on a prope…

Read Post

How to solve the error assets file project.assets.json not found in Visual Studio

This post tells you how to solve the error assets file project.assets.json not found in Visual Studi…

Read Post

Code to help you debug an umbraco issue on a remote site

This post gives you some razor code to help you see the values of the IPublishedContent item's prope…

Read Post

How to fix the 403 error on preview in Umbraco after upgrading

This post shows you how to fix the 403 error on preview after you have upgraded it.

Read Post