I have a very strange issue with IE and ASP.NET MVC 4 Data Annotations.
Some of them seem to be showing their server side error messages on the initial page load only in IE (9 and 9 in 8/7 standards mode). The ones affected seem to be some StringLength and RegularExpression but not all I have other Data Annotations e.g. Required which are not showing their messages.
I have customised how validation messages are inserted client side so they are definitely server side errors.
Does anyone have any idea why this might happen? I am completely confused.
I have tried calling ModelState.Clear() in my action method but this does not seem to resolve the issue..
I am using the P-R-G pattern but the G part which renders my form is a child action, could this be the issue?
Code in response to comment:
public ActionResult MyForm()
{
var cp = GetComponentPresentation();
var model = new MyFormModel(cp);
model.ComponentId = cp.Component.Id;
LoadModel(model);
return PresentationView(model);
}
private void LoadModel(MyFormModel model)
{
string titleTypesTcmId = ConfigHelper.TitleTypesKeywordId.FormatWith(ConfigHelper.PublicationId);
model.TitleKeywords = TaxonomyService.GetTaxonomyKeywordChildrenNames(titleTypesTcmId)
.Select(x => new SelectListItem() { Text = x, Value = x }).ToList();
}
The View Html is 250 lines and I don't have time to anonymise it. So here is a snippet of one of the text boxes that seems to be failing validation.
<div class="row">
@using (Html.BeginForm("MyForm", "MyFormController", FormMethod.Post, new { id = "MyForm", @class = "form ajax", data_ajax_error=Html.GetPlainTextValue(Model.Component, "error_message") }))
{
<input type="hidden" name="ComponentId" value="@Model.Component.Id"/>
<fieldset class="paddingright">
<legend>Your Details</legend>
<div class="row field">
@Html.LabelForRequired(m => m.NationalInsuranceNumber, Html.Resource("Labels_Form_NINumber"))
@Html.TextBoxFor(m => m.NationalInsuranceNumber, new { placeholder=Html.Resource("Placeholder_Form_NINumber") })
@Html.ValidationMessageFor(m => m.NationalInsuranceNumber, null, new { @class = "error"})
</div>
<!-- SNIP -->
<div class="submit-container no-js-hidden">
<button type="submit" value="Step2" class="btn-green customsubmit" name="FormButton">
<span class="content">Next step ></span>
</button>
</div>
</fieldset>
}