I have a form containing a number of controls - nothing fancy:
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<fieldset>
<legend>EmployeeViewModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Employee.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Employee.Title)
etc.....
UPDATE My ViewModel:
public class EmployeeCreateViewModel
{
public EmployeeCreateModel Employee { get; set; }
..etc
My Employee model:
public class EmployeeCreateModel
{
[Required]
public string Title { get; set; }
[DisplayName("Job Title")]
[Required]
public string JobTitle { get; set; }
public bool Active { get; set; }
...etc
The Problem - I am using unobtrusive validation which works fine UNTIL I add a checkbox to the form. Regardless of the checkbox state, the form is submitted, bypassing client-side validation and errors are caught by the server-side validation. This is my checkbox:
<div class="editor-label">
@Html.LabelFor(model => model.Employee.Active)
</div>
<div class="editor-field">
@Html.CheckBoxFor(model => model.Employee.Active)
</div>
The checkbox model property is not a required field, so it doesn't need to be validated and I can see that it has a valid True/False value when it reaches the controller method.
Why is this happening, and how can I fix it?