2010-12-07 17 views
5

Je veux utiliser jQuery ($ .post) pour soumettre mon formulaire html, mais je veux utiliser la fonction de validation côté client de MVC 2. Actuellement, je relie la fonction de publication à la " OnSubmit » cas de la balise form, mais je ne peux pas accrocher dans la validation, idéalement, je veux être en mesure de le faireMVC 2 validation jQuery & formulaire ajax

if (formIsValid) { 
    $.post('<%=Url.Action("{some action}")%>'... 
} 

S'il vous plaît noter, la validation côté client travaille avec jQuery.validation, je ne peux » t l'obtenir pour tester si la validation a été réussie ou non avant que je poste mes données.

Andrew

La solution finale

<% 
    Html.EnableClientValidation(); 
    using (Html.BeginForm("Register", "Account", FormMethod.Post, new { id = "registrationForm" })) { 
%> 
... 
<button type="submit" onclick="return submitRegistration();">Register</button> 
<% 
    } 
%> 



<script type="text/javascript"> 
    function submitRegistration() { 
    if ($("#registrationForm").valid()) { 
     $.post('<%=Url.Action("{some action}")'... 
    } 
    // this is required to prevent the form from submitting 
    return false; 
    } 
</script> 

Répondre

2

Vous pouvez lancer la validation jQuery sur le bouton événement click. Placez les éléments suivants dans votre bouton clic-gestionnaire d'événements:

if ($('form').valid()) 
    //take appropriate action for a valid form. e.g: 
    $('form').post('<%=Url.Action("{some action}")%>') 
else 
    //take appropriate action for an invalid form 

Voir le plug-in Validation documentation pour plus d'informations.