Saturday, January 23, 2016

$(...).valid is not a function in ASP.NET MVC 5

I created an ASP.NET MVC 5 simple application with entry forms integrating bootstrap and jQuery validation. Upon testing, the valid() built-in function is not recognized by the browser as stated in the title of this post. After series of investigation, I found out that by default, ASP.NET MVC does not render the jQuery validation scripts by default in _Layout.cshtml. Only jQuery core scripts.

The fix is to render jqueryval validation scripts in _Layout.cshtml. The scripts have already been bundled in the BundleConfig file.
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval") @*code fix*@
@Scripts.Render("~/bundles/bootstrap")

On page load, the scripts are indeed rendered to the browser using chrome dev tools.

0 comments:

Post a Comment