Sometime ago, I created a surface controller, model and partial for a contact form. Nothing complicated, just a simple form that uses SendGrid to deliver an email on submission.
Somehow, I've broken the hook between the page and the surface controller such that hitting Submit triggers a HTTP POST but then nothing actually happens and I can't figure out why.
Can anyone offer any suggestions as to what stupid thing I have done? FWIW, I'm running Umbraco version 7.4.3.
Surface Controller
using System.Web.Mvc;
using ArcibleWebsite.Models;
using System.Net.Mail;
using SendGrid.Helpers.Mail;
using Newtonsoft.Json;
namespace ArcibleWebsite.Controllers
{
public class ContactSurfaceController : Umbraco.Web.Mvc.SurfaceController
{
[HttpPost]
public ActionResult CreateContactMessage(ContactViewModel contactFormData)
{
if (!ModelState.IsValid)
{
return CurrentUmbracoPage();
}
ViewBag.Contact = contactFormData;
// Global variables for building the messages
SENDGRID STUFF HAPPENS HERE
// return CurrentUmbracoPage();
return RedirectToCurrentUmbracoPage;
}
}
}
@model ArcibleWebsite.Models.ContactViewModel
@using (Html.BeginUmbracoForm<ArcibleWebsite.Controllers.ContactSurfaceController>("CreateContactMessage"))
{
if (!ViewData.ModelState.IsValid)
{
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-8">
@Html.ValidationSummary(true, "Sorry, but there is a problem with the data you've entered. Please check the data you entered and try again.")
</div>
</div>
}
FORM STUFF HAPPENS HERE
<input type="submit" class="btn btn-pink" value="Submit your Question" />
</form>
}
My first thought, is check the rendered HTML and network traffic. It might be doing a GET instead of a POST hence never getting to your controller.
Generally I find it safer to specify the form is a post.
Also, if the controller code isn't complete, make sure you don't have a GET and a POST called the same thing (on the same controller), I find in Umbraco they conflict and it causes issues.
Looking at your controller, it looks like it's missing the inheritance for SurfaceController. So at the moment it's just a regular class and not routed as a controller, which is why your code compiles fine but the forms cannot post to your actionresult.
I'm guessing since it used to work it might be something you've accidentally removed somehow.
Change it to:
public class ContactSurfaceController : SurfaceController
so if you have something different to that ? maybe it's not binding to the properties of the model... ?
Finally it does have some strict validation on the model, eg more than 10 characters for some fields etc, so make sure your validation is showing, I turned on the validation summary
@Html.ValidationSummary(false, "Sorry, ....
to see the messages, but you probably have Html.ValidationFor within your form...
It looks like you have an extra closing </form> tag in there. When using Html.BeginUmbracoForm you shouldn't add any other form tags, otherwise the form won't submit. So check that.
Thanks everyone for the replies. I updated the code, adding the reference to Umbraco.Web.Mvc so that SurfaceController didn't need to be fully qualified and I updated the form tags in the partial.
Everything is now working so I'm not exactly sure which of those two actually resolved it but I suspect it was the form tags.
Surface Controller Not Invoking on Submit
Hi all.
Sometime ago, I created a surface controller, model and partial for a contact form. Nothing complicated, just a simple form that uses SendGrid to deliver an email on submission.
Somehow, I've broken the hook between the page and the surface controller such that hitting Submit triggers a HTTP POST but then nothing actually happens and I can't figure out why.
Can anyone offer any suggestions as to what stupid thing I have done? FWIW, I'm running Umbraco version 7.4.3.
Surface Controller
Model
Partial
Hmm, looks OK at first glance. Have you tried using Visual Studio's debugger to step through your code? That might help show where it loses the call.
My first thought, is check the rendered HTML and network traffic. It might be doing a GET instead of a POST hence never getting to your controller.
Generally I find it safer to specify the form is a post.
Also, if the controller code isn't complete, make sure you don't have a GET and a POST called the same thing (on the same controller), I find in Umbraco they conflict and it causes issues.
Nik
Hi Richard.
Looking at your controller, it looks like it's missing the inheritance for SurfaceController. So at the moment it's just a regular class and not routed as a controller, which is why your code compiles fine but the forms cannot post to your actionresult.
I'm guessing since it used to work it might be something you've accidentally removed somehow.
Change it to:
public class ContactSurfaceController : SurfaceController
Hope this solves the problem.
Have a great weekend!
Hi Dennis.
The reference is there, it's just got a little mangled by the code posted. Here's just that singular line:
Is the fact that I'm having to reference SurfaceController using it's full name and the lack of a using statement for Umbraco.Web.Mvc a problem?
Hi Richard
I just cut and paste what you have into the fanoe starter kit, and it all sort of works...
you shouldn't need the closing
tag though inside your @using (Html.BeginUmbracoForm bit (that will be taken care of by the BeginUmbracoForm itself...
having that on it's own breaks parsing of the partial razor file...
is that the issue ?
If that's not the issue then it could be the way in which you are presenting the form fields, which we can't see:
I just did:
so if you have something different to that ? maybe it's not binding to the properties of the model... ?
Finally it does have some strict validation on the model, eg more than 10 characters for some fields etc, so make sure your validation is showing, I turned on the validation summary
@Html.ValidationSummary(false, "Sorry, ....
to see the messages, but you probably have Html.ValidationFor within your form...
Anyway hope that helps from a sanity perspective.
regards
Marc
It looks like you have an extra closing
</form>
tag in there. When usingHtml.BeginUmbracoForm
you shouldn't add any other form tags, otherwise the form won't submit. So check that.Hi.
Check the form-generated output html:
Then check if you're really hitting controller method with post (console debugger in browser + vs debug)
This code is working for me:
_Contact.cshtml
ContactController.cs
Good luck
Ah i see! Thought I found the obvious answer! :)
Maybe, not sure though. You could try to reference Umbraco.Web.Mvc with the using statement and see if that works, but I'm skeptical..
As Nik mentioned, could you paste in the rendered HTML and maybe we can see what actually gets rendered.
Thanks everyone for the replies. I updated the code, adding the reference to Umbraco.Web.Mvc so that SurfaceController didn't need to be fully qualified and I updated the form tags in the partial.
Everything is now working so I'm not exactly sure which of those two actually resolved it but I suspect it was the form tags.
Many thanks to all.
is working on a reply...