I have a very odd problem with unobtrusive validation.
I have a model with a Required attribute on three stirng properties. I'm using TextBoxFor for two of the fields, and TextAreaFor for the last. The validation is working on the TextArea, but NOT on the input fields. Looking at the source, I can see that the attributes that the validation adds are not on the inputs, but ARE on the text box. Additionally, if I enter data into the text box and submit, it posts and MVC model binding DOES correctly set ModelState.IsValid to false and the ValidationSummary shows the fields.
What would cause the validation to not work on the input elements?
Model:
public class TestModel : IEmail {
public string BCCAddresses {
get;
set;
}
[Required]
public string BodyFormat {
get;
set;
}
public string CCAddresses {
get;
set;
}
[Required]
public string FromAddress {
get;
set;
}
[Required]
public string SubjectLineFormat {
get;
set;
}
}
View:
@section head {
<script src="@Url.Content( "~/Scripts/jquery.1.7.1.min.js" )" type="text/javascript"></script>
<script src="@Url.Content( "~/Scripts/jquery.validate.min.js" )" type="text/javascript"></script>
<script src="@Url.Content( "~/Scripts/jquery.validate.unobtrusive.min.js" )" type="text/javascript"></script>
}
@{
Html.EnableClientValidation();
Html.EnableUnobtrusiveJavaScript();
}
@using ( Html.BeginForm( "Edit", "Email", FormMethod.Post ) ) {
<table>
<tr>
<td>From</td>
<td>@Html.TextBoxFor( x => x.FromAddress, new { style = "width: 500px;" } )</td>
</tr>
<tr>
<td>CC</td>
<td>@Html.TextBoxFor( x => x.CCAddresses, new { style = "width: 500px;" } )</td>
</tr>
<tr>
<td>BCC</td>
<td>@Html.TextBoxFor( x => x.BCCAddresses, new { style = "width: 500px;" } )</td>
</tr>
<tr><td colspan="2"> </td></tr>
<tr>
<td colspan="2">Subject Line</td>
</tr>
<tr>
<td colspan="2">@Html.TextBoxFor( x => x.SubjectLineFormat, new { style = "width: 500px;" } )</td>
</tr>
<tr><td colspan="2"> </td></tr>
<tr>
<td colspan="2">Body Form</td>
</tr>
<tr>
<td colspan="2">@Html.TextAreaFor( x => x.BodyFormat, new { style = "width: 500px; height: 200px" } )</td>
</tr>
<tr><td colspan="2"> </td></tr>
<tr>
<td colspan="2">
<input id="ApplyChanges" type="submit" class="NavigationButton" value="Save Changes" />
<input id="CancelChanges" type="button" class="NavigationButton" value="Cancel Changes" />
</td>
</tr>
<tr>
<td colspan="2">
@Html.ValidationSummary()
</td>
</tr>
<tr><td colspan="2"> </td></tr>
</table>
}
Browser source:
<form action="/url" method="post">
<table>
<tbody><tr>
<td>From</td>
<td><input style="width: 500px;" id="FromAddress" name="FromAddress" value="" type="text"></td>
</tr>
<tr>
<td>CC</td>
<td><input style="width: 500px;" id="CCAddresses" name="CCAddresses" value="" type="text"></td>
</tr>
<tr>
<td>BCC</td>
<td><input style="width: 500px;" id="BCCAddresses" name="BCCAddresses" value="" type="text"></td>
</tr>
<tr><td colSpan="2"> </td></tr>
<tr>
<td colSpan="2">Subject Line</td>
</tr>
<tr>
<td colSpan="2"><input style="width: 500px;" id="SubjectLineFormat" name="SubjectLineFormat" value="" type="text"></td>
</tr>
<tr><td colSpan="2"> </td></tr>
<tr>
<td colSpan="2">Body Form</td>
</tr>
<tr>
<td colSpan="2"><textarea style="width: 500px; height: 200px;" id="BodyFormat" cols="20" rows="2" name="BodyFormat" data-val-required="The BodyFormat field is required." data-val="true"></textarea></td>
</tr>
<tr><td colSpan="2"> </td></tr>
<tr>
<td colSpan="2">
<input id="ApplyChanges" class="NavigationButton" disabled="disabled" value="Save Changes" type="submit">
<input id="CancelChanges" class="NavigationButton" value="Cancel Changes" type="button">
</td>
</tr>
<tr>
<td colSpan="2">
<div class="validation-summary-valid" data-valmsg-summary="true"><ul><li style="display: none;"></li>
</ul></div>
</td>
</tr>
<tr><td colSpan="2"> </td></tr>
</tbody></table>
</form>