1

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

ok, i see, but i have all the scripts included and also i have ClientValidationEnabled and UnobtrusiveJavaScriptEnabled enabled.
i forgot that MVCContrib does not generate Unobtrusive Validation Attributes, thats whyit is does not work for me. weblogs.asp.net/srkirkland/archive/2011/03/08/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.