Validating form using jquery Ajax MVC - asp.net-mvc-3

Up to this point, I’ve been using Microsoft Ajax for updates in my MVC app. I’m now switching to jQuery Ajax in order to standardize my approach.
However, I just realized that the required field in my model for the form did not fire and the user was able to submit the form even though some required fields were left blank.
What is the standard way to handle required fields if I’m handling my ajax functions using jQuery – i.e. I want the unobtrusive to handle my required fields by highlighting them and not allow the submit button to be clicked.
Also, to mention I am getting data-val attributes through the model.
P.S. I’m now using an event handler to detect click event on submit button for the form. In other words, I’m not using #using.Ajax.BeginForm to define my form.

Related

spring form submission in a popup

I have a small problem in my first spring&hibernate application.
In my page, I would like to create a popup window (modal) which gives the possibility to send an email. So, this popup would display a spring form with a fied "subject" and a field "content".
The question is : how can I use the form binding mechanism of spring with this popup form if I don't want my main page (displayed behind the popup form) to be reloaded?
If i am not wrong, the content of the popup is a div present in the main page. So, If I submit the form in the popup, the whole page will be reloaded (through the mvc mechanism of spring) and this is exactly what I don't want.
Of course, I can submit my form via an ajax call to a spring controller, but in this case, how to handle the eventual errors in the form? I cannot use the spring form binding mechanism to display the errors on the popupform. Is it possible?
Do you know if there is a way to open a new window in a modal popup? So that this page and only this page would be reloaded during the submission of the form?
Actually, I am not sure of the best way to combine a popup containing a form and the form submission mechanism of spring. How to display the errors on the form in case of errors? after reload of the page, If there are some errors, I need to open my popup again in javascript and then display the errors on the popup, this is the only mean I see, but maybe there are easier solutions.
If you have, I would be happy to know it.
thanks a lot

MVC 4, Jquery Mobile, Ajax.BeginForm causing forms to submit twice

Does anyone know of any bugs or other insight to explain why when using MVC4 with jquery mobile, any form that is created using Ajax.BeginForm results in the form being submitted twice to the controller.
I had originally thought that since I am using the js bundling that the same js file might have been being included twice and might be triggering on form submit, however thats not the case, my list of js files are:
jquery-1.6.4.js
jquery-ui.1.8.11.js
jquery.mobile-1.1.0.js
jquery.unobtrusive-ajax.js
jquery.validate.js
knockout-2.0.0.js
MicrosoftAjax.js
MicrosoftMvcAjax.js
MicrosoftMvcValidation.js
Modernizr.js
Within the project Ajax is fully enabled for everything i.e. i'm not disabling anything through mobileinit.
I have not posted any form code because it literally happens with every form - a form with one field and a submit button will cause the submission twice - but only where Ajax.BeginForm is being used.
Html.BeginForm doesn't exhibit any of these problems.
I've been stuck on this for a few days now so any help would be greatly appreciated
jQuery Mobile by default hi-jacks any form submission and performs its own AJAX request. To stop this behavior you can place the data-ajax="false" attribute on any <form> tag. Also make sure to stop the regular behavior of the form so it doesn't submit normally, something like:
$('#my-form').on('submit', false);
Docs: http://jquerymobile.com/demos/1.1.0/docs/forms/forms-sample.html
You could also just use the built-in jQuery Mobile AJAX rather than including extra JS for it. Which would only require you to make the action attributes of the forms to the server-side file to which they posts.

MVC 3 - AutoPostback with CauseValidation=false Behavior

I'm familiar with the method of submitting a form via a change event like or a checkbox's check event. Most articles explain how to find the first form using jQuery and invoke it's submit() method. However, I wish to submit the form but not cause validation so the back-end can perform other actions. In my case, I have a dropdown that a users changes. Depending on the value, the back-end adds/removes certain controls. The issue I'm runing into is that the validation prevents the form from submitting.
Q: How do I submit the form so it does not perform client-side validation?
Remove the validation just before the form is submitted - check out the answers here How do I remove jQuery validation from a form?
I think you can just turn the validation off by calling #Html.EnableClientValidation(false)

Asp.net MVC Ajax - Disabling button in ajax.beginform?

I'm using microsoft ajax and ajax.beginform.
It's working great for model binding my partial view, but I need to conditionally disable some of the buttons in my form itself depending on what comes back from the server.
How can I go about this without using JQuery?
Have you thought instead about using two separate views instead, and have your conditional logic in your controller determine which view to display.

Unobtrusive Javascript Validation with MVC 3, is preventing me validating form manually

For 90% of my site the standard MVC annotation with client script method is working a treat. But I have a form on the site that is quite complicated with multiple instances of dynamic form content dependant on answers to questions etc.
If I have the unobtrusive script included on the page, it's capturing the form submit and not allowing my custom jquery validate to validate the form.
I don't really want to refactor the site to have a seperate layout to remove the script when it's not needed. I wondered if there was an easy way to give control back to my custom validate script.
Any help would be great.
In your view you can disable client side validation like this
Html.ViewContext.ClientValidationEnabled = false

Resources