Vasya Pupkin
Vasya Pupkin

Reputation: 645

Getting error when validationg ASP.NET MVC 3.0 AjaxForm with jQuery

Trying to use this solution to validate AjaxForm ASP.Net MVC Ajax form with jQuery validation but i am getting error: Uncaught exception: TypeError: Cannot convert '$('MyFrom').validate()' to object

My from:

@using (Ajax.BeginForm("MyFromAction", "Something", null, new AjaxOptions() { OnBegin = "onBeginMyFrom", OnFailure = "onFailureMyFrom", OnSuccess = "onSuccessMyFrom" }, new { @id = "MyFrom" }))
{
}

My form was working correctly and posting to the server, but after i use jQuery validation i am getting error above.

Any ideas?

UPDATE: I didn't mention that i am using MVCContrib FluentHtml.

MVCContrib required this http://weblogs.asp.net/srkirkland/archive/2011/03/08/adding-unobtrusive-validation-to-mvccontrib-fluent-html.aspx to get Unobtrusive validation work.

Is MvcContrib will support Unobtrusive validation in the next release by default?

Upvotes: 1

Views: 355

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

The answer you are looking at was related to a previous version of ASP.NET MVC. In ASP.NET MVC 3 client validation is performed using the jquery validate plugin and it is done in an unobtrusive matter. You don't need any code for this. So the first thing is to make sure that you have included the proper scripts:

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

then that unobtrusive javascript and validation are enabled in web.config:

<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>

Now you can have your AJAX form behave properly:

@using (Ajax.BeginForm("MyFromAction", "Competition", null, new AjaxOptions() { OnBegin = "onBeginMyFrom", OnFailure = "onFailureMyFrom", OnSuccess = "onSuccessMyFrom" }, new { @id = "MyFrom" }))
{
    ...
}

The form will check for validation errors at the client side and it will not be submitted until they are fixed.

Upvotes: 1

Related Questions