I am having problems with MVC RC2 where upon validation failure, the failed field will throw a NullReferenceException when the view is passed back to the user.
A short term solution was found: which was to rename the Html.ValidationMessage to be different than the Target Form Field. This works!
BUT now the automatic highlighting is disconnected from the input field. (Out of the box behavour is to change the target field's CSS Class which makes it standout)
So...
What is the actual problem with my code? AND Why wont it allow my ValidationMessage and Form fields share the same names?
The code is throwing NullReferenceException when the following code is run:
View Code
<% using (Html.BeginForm()) { %>
<fieldset>
<h5>Terms and Conditions</h5>
<p>
<%= Html.CheckBox("Terms", false)%>
<%= Html.ValidationMessage("Terms")%>
I agree to the <a href="/signup/terms">Terms & Conditions.</a>
</p>
</fieldset>
<input class="signup_button" type="submit" title="Sign Up" value="" />
<% } %>
<%= Html.ValidationSummary("Sign up wasn't successful.")%>
Controller Code
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection form)
{
bool Terms = form["Terms"].ToString() == "true,false" ? true : false;
if (Terms)
{
return RedirectToAction("Success", "Signup");
}
else
{
ModelState.AddModelError("Terms", "Please agree to the Terms");
ModelState.AddModelError("_FORM", "Terms not checked");
}
return View();
}
I can get the code to work if I omit the following:
ModelState.AddModelError("Terms", "Please agree to the Terms");
But with this, the checkbox throws the Null reference exception.
Any Ideas?