I'm using a mostly new jquery validation stack with ASP.NET MVC 5
- jquery-1.10.2
- jquery.validate 1.13.1
- jquery.unobtrusive-ajax.js 3.2.2
- jquery.validate.unobtrusive.js 3.2.2
I'm using an Ajax form with a partial view and for some reason the compare validation attribute works on the first post but not on subsequent posts. Here is my example:
Model
public class TestModel
{
[Required]
public string NewPassword { get; set; }
[Required]
[Compare("NewPassword")]
public string ConfirmPassword { get; set; }
}
Index.cshtml
@model TestModel
<h2>Index</h2>
@Html.Partial("_TestForm", Model)
_TestForm.cshtml
@model TestModel
@using (Ajax.BeginForm("FormTester", "Test", new AjaxOptions{UpdateTargetId = "formContainer"}))
{
@Html.AntiForgeryToken()
<div id="formContainer">
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.NewPassword, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.NewPassword)
@Html.ValidationMessageFor(model => model.NewPassword)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ConfirmPassword)
@Html.ValidationMessageFor(model => model.ConfirmPassword)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
</div>
}
And finally my test controller
public class TestController : Controller
{
public ActionResult Index()
{
return View(new TestModel());
}
// GET: Test
public ActionResult FormTester(TestModel model)
{
if(ModelState.IsValid)
{
return PartialView("_TestForm");
}
return PartialView("_TestForm", model);
}
}
Steps to reproduce,
- Submit the form with valid data.
- When the form is returned, there should not be any validation messages:
- Change both fields again to be the exact same but with different data and attempt to submit:
- Now the only way to submit the form again is to change the second field (or both) to be the original value:
I have no idea why this is happening or if there is even a workaround for this. Please help.