This is a strange one. I have a Surface controller for an Ajax contact form. I integrated reCaptcha on that form and have an issue. When the reCaptcha is invalid, all logic works and I return a Json value of “Success = false” but when returned to the PartialView, it fires the OnSuccess script and not the OnFailure script. The correct scripts are fired when the form is successful.
Here is the controller code:
public class ContactFormController : SurfaceController
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult PostContactFormResults(Models.ContactFormModel model)
{
if (!ModelState.IsValid)
{
return Json(new { success = false });
}
string EncodedResponse = Request.Form["g-Recaptcha-Response"];
ReCaptcha rc = new ReCaptcha();
rc.PrivateKey = ConfigurationManager.AppSettings["rcSecretKey"];
bool IsCaptchaValid = (rc.Validate(EncodedResponse) == "True" ? true : false);
if (!IsCaptchaValid)
{
return Json(new { success = false });
}
var service = ApplicationContext.Services.ContentService;
var config = service.GetById(common.GetConfigurationId("ew"));
string emailFrom = config.GetValue("contactUsFromEmailAddress").ToString();
string emailTo = config.GetValue("contactUsEmailRecipients").ToString();
string emailSubject = config.GetValue("contactUsSubjectLine").ToString();
string messageTemplate = config.GetValue("contactUsEmailTemplate").ToString();
string _FirstName = model.FirstName;
string _LastName = model.LastName;
string _PhoneNumber = model.PhoneNumber;
string _EmailAddress = model.EmailAddress;
string _Company = model.Company;
string _State = model.State;
string _City = model.City;
string _Country = model.Country;
string _ZipPostalCode = model.PostalCode;
string _BusinessUnit = model.BusinessUnit;
string _Comments = model.Comments;
string _DateTimeNow = DateTime.Now.ToString();
string[] messageVariables = { "[FirstName]", "[LastName]", "[PhoneNumber]", "[EmailAddress]", "[BusinessUnit]", "[Company]", "[Country]", "[City]", "[State]", "[PostalCode]", "[DateTimeNow]" };
string[] modelVariables = { _FirstName, _LastName, _PhoneNumber, _EmailAddress, _BusinessUnit, _Company, _Country, _City, _State, _ZipPostalCode, _DateTimeNow };
for (var i = 0; i < messageVariables.Length; i++)
messageTemplate = messageTemplate.Replace(messageVariables[i], modelVariables[i]);
Messaging msg = new Messaging();
msg.EmailFrom = emailFrom;
msg.EmailTo = emailTo;
msg.EmailSubjectLine = emailSubject;
msg.EmailBody = messageTemplate;
bool result = msg.SendMailMessage();
return Json(new { success = result });
}
}
This is in the header of the PartialView:
@model MyNameSpace.Models.ContactFormModel
@using (Ajax.BeginForm("PostContactFormResults", "ContactForm", null, new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "submit-status", OnFailure = "ShowContactError()", OnSuccess = "ShowContactSuccess()" }, new { @id = "frmContact" }))
{
Can anyone see an obvious mistake? I have 2 forms that are doing this.
Thanks
Phillip
This is a companion discussion topic for the original entry at https://our.umbraco.com/forum/77177-ajax-form-json-success-is-false-but-returns-true