MVC3 jQuery.validate not working when submitting form
I've been working in MVC3 using the built in jquery validation.
Client validation did appear to be working when tabbing through the fields except when I submitted the actual form.
Below are my include scripts:
<script src="@Url.Content("~/Scripts/jquery-1.6.2.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>
I managed to fix this by reverting the jquery version to jquery-1.4.4.min.js so I'm just wonder开发者_StackOverflow社区ing:
Isn't jquery backwards compatible? Also - why would the validation actually work but just fail when an input submits?
Could this be a bug in the new version of jQuery?
At first it seemed to me like jQuery 1.6 would break compatibility. It turned out it was just a little trickier to get unobtrusive validation working than I thought. This is a good tutorial that'll get you a working example:
http://www.asp.net/mvc/tutorials/creating-a-mvc-3-application-with-razor-and-unobtrusive-javascript
You can then switch to jquery 1.6.2 and see that it's still working. For me, what was missing from my nearly identical view was Html.BeginForm. Once I added that, everything started working.
I think it is a bug and there is no solution for this.
Is the form posting? If not you may have a validation rule that's being violated but you may not see the error message if you don't have Html.ValidationSummary or Html.ValidationMessageFor enabled.
**EDIT:
I tried with the latest version of jQuery (1.6.4) and with 1.6.2 with this code without any issues.
controller code
public class TestViewModel
{
[Required]
public string textTest { get; set; }
[Required]
public int intTest { get; set; }
}
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new TestViewModel());
}
[HttpPost]
public ActionResult Index(TestViewModel model)
{
if (ModelState.IsValid)
{
return Redirect("http://www.yahoo.com");
}
return View(model);
}
}
markup
@model MvcApplication39.Controllers.TestViewModel
@{
ViewBag.Title = "Index";
}
<h2>ViewBag.Title</h2>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.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>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>TestViewModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.textTest)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.textTest)
@Html.ValidationMessageFor(model => model.textTest)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.intTest)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.intTest)
@Html.ValidationMessageFor(model => model.intTest)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
精彩评论