MVC表单POSTING null模型

时间:2015-11-01 20:19:23

标签: asp.net-mvc model-view-controller umbraco

这个Umbraco表单渲染得很好,但无法正确地发布我的模型 - 当在表面控制器中进行断点操作时,所有模型属性都为空。

我错过了一些小事吗?

模型

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;
}
}

表面控制器

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);
    }
}
}

查看

@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 })

部分(联系表格本身)

@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 个答案:

答案 0 :(得分:3)

首先,您的模型应该具有属性,而不是字段:

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

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

可能就是这样。