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();
}
}
}
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."
Hi Stephen,
Try the following instead:
Thanks, Dan.
Still get it using code below.
Hi Stephen,
I think the line getting the homepage is incorrect. Try something like...
Thanks, Dan.
Thanks Dan.
is working on a reply...