I’ve faced the following problem. I’m developing a form for the site and this form should have validation. I wanna to use native ASP.NET MVC 2 validation functionality but get stubborn with it. I have a form that is loaded via $.get and displayed using jQuery UI modal dialog. All examples I found explains how to use MVC validation with simple forms and avoid Ajax forms.
I can enable client side validation for this form, but I need to handle server-side validation correctly. How can I handle server-side validation model errors for ajax forms?
When you pass your object back to the controller, you have to wrap your code in
If ModelState.IsValidBelow is a simplified version of how I edit a user. The first “EDIT” sends the User object TO the View. The second “EDIT” handles the post from the view.
Here’s the same thing in C#
EDIT:
On your view, if you want to add AJAX validation, just add the following.
EDIT:
Here is some info on rendering using
Ajax.BeginFormhttp://singulartechnologies.com/asp-net-mvc-ajax-beginform-sample-code
http://msdn.microsoft.com/en-us/library/dd381533.aspx
http://weblogs.asp.net/mikebosch/archive/2008/02/15/asp-net-mvc-submitting-ajax-form-with-jquery.aspx