Over a million developers have joined DZone.

Fixing Client Validation in MVC 3 and MVC 4 (Beta)

DZone's Guide to

Fixing Client Validation in MVC 3 and MVC 4 (Beta)

Free Resource

Recently while working on an application I ran into an client side validation issue. The issue was that ASP.NET MVC 3 application ignoring the cancel class(which is used to disable the validation of a particular submit button), when you use unobtrusive ajax(for example, Ajax.BeginForm helper). This issue is also present in ASP.NET MVC 4 Beta at two places. Ben Reich raised this issue at http://aspnet.codeplex.com/workitem/7964. In this article, I will show you the issue and a quick solution.


To understand this issue just add s Ajax.BeginForm helper with a submit button in your ASP.NET MVC 3(or ASP.NET MVC 4) application. Set the submit button class to 'cancel'. Run this application. You will find that the application is ignoring 'cancel' class(which is used to disable validation). Similarly, add a submit button with 'cancel' class inside new Login popup of ASP.NET MVC 4 Beta application, you will get the same issue. To resolve this issue just open AjaxLogin.js file(for only ASP.NET MVC 4 Beta) and replace this:


with this:

.click(function () {
        $(this).closest('form').data('elem', $(this));

and then replace this:

if (!$form.valid || $form.valid()) {


var $target = $form.data('elem');
if ($target.hasClass('cancel') || !$form.valid || $form.valid()) {

Then just open jquery.unobtrusive-ajax(.min).js file(both in MVC 3 and MVC 4) and replace this:

$(form).data(data_click, name ? [{ name: name, value: evt.target.value}] : []);


$(form).data("elem", $(evt.target));
$(form).data(data_click, name ? [{ name: name, value: evt.target.value}] : []);

and then replace this:

if (!validate(this)) {


var $form = $(this), $target = $form.data("elem");
if (!$target.hasClass("cancel") && !validate(this)) {

Now run the application you will find that the issue is resolved.


In this article, I showed you a client side validation issue in ASP.NET MVC 3 and ASP.NET MVC 4 Beta. I also showed how to quickly resolve this issue. Hopefully you will enjoy this simple article too.


Source: http://weblogs.asp.net/imranbaloch/archive/2012/02/22/a-client-validation-issue-in-mvc-3-and-mvc-4-beta.aspx


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}