Ajax post a form (validation) in MVC AsP.net?

ecco88 picture ecco88 · Feb 27, 2014 · Viewed 15.3k times · Source

So I have a page that have a few forms on it and one of those forms is a one for addresses which has three functions. Save as new, Update and Delete. I want to use form.submit() to fire the MVC validation on the client and server side but I dont want the entire page refresh.

So this is what I want to use the simple form.submit() to gather the elements and send it to the ASP.net MVC controller as a model and handle the validation based on the attributes I have on the model and use a callback to handle a JSON response to update only the affected area.

I know I could use jQuery to create an AJAX post request but I would have to handle everything, i.e. bind to the viewmodel, validation and create the request itself. Is there anyway I can get the best of both methods?

Answer

Nic picture Nic · Feb 27, 2014

Also do not forget in Post Method in Controller to check

if(ModelState.IsValid) 

because client side validation will not validate hidden fields also if you will use ctr+shift+i in any browser and you will delete an @Html.EditorFor(model => model.Price) client validation will not validate this attribute.