2017-05-08 3 views
1

Ich habe eine Seite mit einer Ajax-Form darauf. Wenn ich das Submit drücke, sollte es ein Lade Gif laden. Siehe das folgende HTML und javascript:asp.net MVC Form Validierung in Javascript

@ using (Ajax.BeginForm ("ActionName", "ControllerName", null, neue AjaxOptions {HttpMethod = "POST", UpdateTargetId = "DivToUpdate"}, neue {ID = "myForm "})) { @ Html.AntiForgeryToken();

@Html.LabelFor(model => model.PagerSize) 
    @Html.EditorFor(model => model.PagerSize, new { htmlAttributes = new { @class = "form-control myForm", style = "width:100px" } }) 
    @Html.ValidationMessageFor(model => model.PagerSize) 

<input id = "ajaxFormSubmitButton" type = "submit" /> 
} 

<script> 
$("#myForm").submit(function (event) { 

      DisplayLoading(); 

     }); 

</script> 

Es funktioniert gut, bis mein Modell nicht gültig ist. Zum Beispiel, wenn ich eine Pagergröße außerhalb meines Bereichs habe, wird mein Lade-GIF angezeigt, da die Seite nichts anderes lädt. Ist es möglich, das DisplayLoading nur bei ModelState.IsValid zu laden? so etwas wie diese

Zum Beispiel:

<script> 
$("#myForm").submit(function (event) { 
If (MyMagicVariableWhichKnowsMyModelStateIsValid){ 
      DisplayLoading(); 
} 
     }); 

</script> 
+0

Pls Format, um die Frage – User3250

Antwort

2

Sie Formular überprüfen gilt wie folgt:

$("#myForm").submit(function (event) { 
if ($("#myForm").valid()){ 
     DisplayLoading(); 
} 
}); 
+0

Danke, das war genau das, was ich suchte. – user1408786

+0

Pls als Antwort, wenn es geholfen hat;) Danke – User3250

+0

Wird es tun, sobald es möglich ist – user1408786

Verwandte Themen