0
votes

This Umbraco form renders fine, but fails to POST my model correct - when breakpointing in the surface controller, all of the models properties are null.

Am i missing something small?

Model

namespace Fairy_Cottage.Models
{
public class ContactFormModel
{
    [Required]
    public string Name;

    [Required]
    [EmailAddress]
    public string EmailAddress;

    [Required]
    public string TelephoneNumber;

    [Required]
    public string Message;

    public int ThankYouPage;
}
}

Surface Controller

namespace Fairy_Cottage.Controllers
{
public class ContactFormSurfaceController : SurfaceController
{
    [HttpPost]
    [ValidateAntiForgeryToken()]
    public ActionResult HandleFormSubmit(ContactFormModel Model)
    {
        if (!ModelState.IsValid)
        {
            return CurrentUmbracoPage();
        }

        //send email

        return RedirectToUmbracoPage(Model.ThankYouPage);
    }
}
}

View

@using Fairy_Cottage.Models
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
Layout = "Master.cshtml";
 }
@Model.Content.Name
@Model.Content.GetPropertyValue("content")

@Html.Partial("~/Views/Partials/_ContactForm.cshtml", new     ContactFormModel { ThankYouPage = 1061 })

Partial (contact form itself)

@using Fairy_Cottage.Controllers
@model Fairy_Cottage.Models.ContactFormModel

@using (var form = Html.BeginUmbracoForm<ContactFormSurfaceController>("HandleFormSubmit"))
{
@Html.AntiForgeryToken()

<div class="row">
    <div class="col-xs-12 col-md-6 col-md-offset-3">
        <div class="">
            <label class="form-label" for="">Contact Name @Html.ValidationMessageFor(m => m.Name)</label>
            <div class="input-group">
                @Html.TextBoxFor(m => m.Name, new { @class = "form-field", placeholder = "Enter Name", required = "required" })
            </div>
        </div>
        <div class="">
            <label class="form-label" for="">Telephone Number @Html.ValidationMessageFor(m => m.TelephoneNumber)</label>
            <div class="input-group">
                @Html.TextBoxFor(m => m.TelephoneNumber, new { @class = "form-field", placeholder = "Enter Telephone Number", required = "required" })
            </div>
        </div>
        <div class="form-group">
            <label class="form-label" for="">Email Address @Html.ValidationMessageFor(m => m.EmailAddress)</label>
            <div class="input-group">
                @Html.TextBoxFor(m => m.EmailAddress, new { @class = "form-field", placeholder = "Enter Email", required = "required" })
            </div>
        </div>
        <div class="form-group">
            <label class="form-label" for="">Message @Html.ValidationMessageFor(m => m.Message)</label>
            <div class="input-group">
                @Html.TextAreaFor(m => m.Message, new { @class = "form-field", rows = 8, required = "required" })
            </div>
        </div>

        @Html.HiddenFor(x => x.ThankYouPage)

        <input type="submit" name="submit" id="submit" value="Submit">
    </div>
</div>
}
1

1 Answers

3
votes

For one, your model should have properties, not fields:

[Required]
public string Name { get; set; }

[Required]
[EmailAddress]
public string EmailAddress { get; set; }

It's possible that's all it is.