Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Stephen Davidson 216 posts 392 karma points
    Oct 10, 2014 @ 18:41
    Stephen Davidson
    0

    Object reference not set to an instance of an object in Surface Controller

    I have the following surface controller. Which gets a null exception on the follwoing line. When running in VS debug mode i get "Object reference not set to an instance of an object."

     var sendEmailsFrom = homeNode.GetPropertyValue("contactEmailFrom") ?? "[email protected]";

     

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Web.Hosting;
    using System.Web.Mvc;
    using System.Xml;
    using Umbraco.Core.Models;
    using Umbraco.Web;
    using cericoumbraco.Models;
    
    namespace cericoumbraco.Controllers
    {
        public class ContactController : Umbraco.Web.Mvc.SurfaceController
        {
            [ChildActionOnly]
            public ActionResult ContactForm()
            {
                var model = new ContactModel();
    
                //Initialize model however you want
    
                //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 homeNode = currentNode.AncestorsOrSelf(0).FirstOrDefault();
    
                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).FirstOrDefault();
    
    
                    var sendEmailsFrom = homeNode.GetPropertyValue("contactEmailFrom") ?? "[email protected]";
                    var sendEmailsTo = homeNode.GetPropertyValue("contactEmailTo") ?? "[email protected]";
    
    
                    var body = String.Format("There has been an enquiry from the xxx website 

    Name: {0} (@ {1})
    Email: {2}
    Tel: {3}

    Message: {4}", model.FullName, model.Company, model.Email, model.Telephone, model.Message); var subject = "XXX website enquiry"; 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.Company = 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"] = "There was an issue sending your message: " + ex.Message + ex.StackTrace; } } return CurrentUmbracoPage(); } } }

     

  • Dan Lister 416 posts 1974 karma points c-trib
    Oct 10, 2014 @ 19:06
    Dan Lister
    1

    Hi Stephen,

    Try the following instead:

    var sendEmailsFrom = homeNode.HasValue("contactEmailFrom") ? homeNode.GetPropertyValue("contactEmailFrom") : "[email protected]";
    

    Thanks, Dan.

  • Stephen Davidson 216 posts 392 karma points
    Oct 10, 2014 @ 19:15
    Stephen Davidson
    0

    Still get it using code below.

    var sendEmailsFrom = homeNode.HasValue("contactEmailFrom") ? homeNode.GetPropertyValue("contactEmailFrom") : "[email protected]";
  • Dan Lister 416 posts 1974 karma points c-trib
    Oct 13, 2014 @ 10:48
    Dan Lister
    100

    Hi Stephen,

    I think the line getting the homepage is incorrect. Try something like...

    if (ModelState.IsValid)
    {
        var homeNode = CurrentPage.AncestorsOrSelf(1).FirstOrDefault();
    
        var sendEmailsFrom = homeNode != null && homeNode.HasValue("contactEmailFrom") 
            ? homeNode.GetPropertyValue("contactEmailFrom") 
            : "[email protected]"; 
    }
    

    Thanks, Dan.

  • Stephen Davidson 216 posts 392 karma points
    Oct 20, 2014 @ 18:25
    Stephen Davidson
    0

    Thanks Dan.

Please Sign in or register to post replies

Write your reply to:

Draft