我有一个 MVC Ajax 表单,其中包含由表单模型类绑定的文本框数据。在这堂课中,我有这些领域:
[Required(ErrorMessage = "First name must be filled")]
public string FirstName { get; set; }
[Required(AllowEmptyStrings = false,ErrorMessage = "Last Name must be filled")]
public string LastName { get; set; }
[DataType(DataType.EmailAddress)]
[Required(AllowEmptyStrings = false, ErrorMessage = "Email must be filled")]
public string Email { get; set; }
所有三个字段都绑定了@Html.EditorFor(p=>p.FirstName/LastName/Email)
- 所以这应该很简单。
一切正常,但是当表单发布时,电子邮件字段始终为空。我试图用<input type="email"
(...)替换它,它也是空的。
所以结论是,当一个表单成员有type="email"
时,值为空。
有谁知道为什么,更重要的是:如何解决?(顺便说一下,这是asp.mvc4)
编辑:这是我的观点:
<div id="result"></div>
@using (Ajax.BeginForm("FormTest", new AjaxOptions() {HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "result"}))
{
{
Html.EnableClientValidation(true);
}
@Html.ValidationSummary(false)
@Html.LabelFor(p => p.EventForm.FirstName)
@Html.EditorFor(p => p.EventForm.FirstName)
@Html.LabelFor(p => p.EventForm.LastName)
@Html.EditorFor(p => p.EventForm.LastName)
@Html.LabelFor(p => p.EventForm.PhoneNumber)
@Html.EditorFor(p => p.EventForm.PhoneNumber)
@Html.LabelFor(p => p.EventForm.Email)
@Html.EditorFor(p => p.EventForm.Email)
@Html.LabelFor(p => p.EventForm.CompanyName)
@Html.EditorFor(p => p.EventForm.CompanyName)
@Html.LabelFor(p => p.EventForm.Adress)
@Html.EditorFor(p => p.EventForm.Adress)
@Html.LabelFor(p => p.EventForm.Zip)
@Html.EditorFor(p => p.EventForm.Zip)
@Html.LabelFor(p => p.EventForm.PostalCity)
@Html.EditorFor(p => p.EventForm.PostalCity)
@Html.LabelFor(p => p.EventForm.Country)
@Html.EditorFor(p => p.EventForm.Country)
@Html.LabelFor(p => p.EventForm.Comment)
@Html.EditorFor(p => p.EventForm.Comment)
}
我的控制器动作:
[HttpPost]
public PartialViewResult Index(EventViewModel form)
{
StoreEventRegistration(form.EventForm);
return PartialView("PagePartial/FormSubmit", form.EventForm);
}
正如您可能理解的那样,我没有添加我的整个模型,但其余字段的创建方式与上面提到的相同。