Reputation: 2400
I'm trying to build a contact form in umbraco v6 by following the following this tutorial. The problems I'm having are that a) The form seems to be posting on load ie. I'm presented with required field errors on the form by default and b) when I actually populate the data and submit, I get a 404 not found error with the following location: Requested URL: /umbraco/RenderMvc
Here is the code I have for my Model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.Security;
using umbraco.cms.businesslogic.member;
using System.ComponentModel.DataAnnotations;
namespace CustomControls
{
public class ContactFormModel
{
[Required, Display(Name = "First name")]
public string FirstName { get; set; }
[Required, Display(Name = "Surname")]
public string Surname { get; set; }
[Required, Display(Name = "Company")]
public string Company { get; set; }
[Required, Display(Name = "Email address")]
[DataType(DataType.EmailAddress)]
public string EmailAddress { get; set; }
[Required, Display(Name = "Telephone")]
public string Telephone { get; set; }
}
}
Here is my SurfaceController code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.Security;
using umbraco.cms.businesslogic.member;
using System.ComponentModel.DataAnnotations;
namespace CustomControls
{
public class ContactFormSurfaceController : Umbraco.Web.Mvc.SurfaceController
{
[HttpGet]
[ActionName("ContactForm")]
public ActionResult ContactFormGet(ContactFormModel model)
{
return PartialView("ContactForm", new ContactFormModel());
}
[HttpPost]
[ActionName("ContactForm")]
public ActionResult ContactFormPost(ContactFormModel model)
{
//model not valid, do not save, but return current umbraco page
if (!ModelState.IsValid)
{
//Perhaps you might want to add a custom message to the ViewBag
//which will be available on the View when it renders (since we're not
//redirecting)
TempData.Add("Status", "Invalid form data");
//return CurrentUmbracoPage();
return RedirectToCurrentUmbracoPage();
}
//if validation passes perform whatever logic
//In this sample we keep it empty, but try setting a breakpoint to see what is posted here
//Perhaps you might want to store some data in TempData which will be available
//in the View after the redirect below. An example might be to show a custom 'submit
//successful' message on the View, for example:
TempData.Add("Status", "Your form was successfully submitted at " + DateTime.Now);
//redirect to current page to clear the form
return RedirectToCurrentUmbracoPage();
//Or redirect to specific page
//return RedirectToUmbracoPage(1063);
}
}
}
And finally here is my view:
@model CustomControls.ContactFormModel
@using(Html.BeginUmbracoForm("SubmitContactForm", "ContactFormSurface"))
{
@Html.EditorFor(x => Model)
<input type="submit"/>
}
<p class="field-validation-error">@TempData["Status"]</p>
This is the snippet of code I'm using in my template to render the form:
@Html.Action("ContactForm","ContactFormSurface")
Is anyone able to point out where I'm going wrong?
Thanks
Upvotes: 2
Views: 5545
Reputation: 1
Have you tried to move the partials to ~/Views/Shared folder?
As for the partial rendering in a view, this worked for me.
@Html.Action(actionName: "ContactForm", controllerName: "ContactFormSurface")
Upvotes: 0
Reputation: 2400
Aha, I found the problem. In my view I'm using the line:
@using(Html.BeginUmbracoForm("SubmitContactForm", "ContactFormSurface"))
Yet "SubmitContactForm" doesn't exist - I forgot to update the name when I updated the names of my methods :-)
Upvotes: 1
Reputation: 10400
In this scenario, instead of rendering as a child action:
@Html.Action("ContactForm","ContactFormSurface")
you should render the view as a partial view:
@Html.Partial("ContactFormSurface/ContactForm")
... pointing to where the view is located.
Upvotes: 1