Loading image not working

Total Post:378

Points:2694
 1424  View(s)
Ratings:
Rate this:

I'm trying to display the loading image when the server is processing. 

It actually works fine when I haven't made any mistakes when filling the form, but when I miss a required field 

for example, the loading image pops up and it doesn't go away and the validation of the field is made.

 I've tried to use jQuery to do something like this:

 

    $(function () {
    $("#button").click(function () {
    alert(form.valid());
    if (form.valid()) {
        $("#divLoading").show();
        $.post({
            type:'POST',
            url: url,
            data:data,
            success: function (data) {
                $("#divLoading").hide();
            }
        });
    } else {
        alert("Not valid.");
    }
    //}
});
});

 

So, when I ask if the form is valid it returns true, in spite of leaving a required field blank. 

My model is the following: 

    public class MotivoDTO
    {
        public MotivoDTO()
        {
        }
        [Required(ErrorMessage = "You must enter a Motive.")]
        [DataType(DataType.Text)]
        [Display(Name= "Descripcion")]
        public string Descripcion { get; set; }
 
    }

Thank you!

  1. Post:135

    Points:947
    Re: Loading image not working

    try this:

    form.validate();

    if (form.valid()) {


  1. Post:341

    Points:2389
    Re: Loading image not working

    Update your .ajax() call to make use of the different events available to you inside of the ajax() function.

     

    $(function () {

        $("#button").click(function () {

        alert(form.valid());

        if (form.valid()) {

     

            $.post({

                type: 'POST',

                url: url,

                data: data,

                //do this before you start the POST method

                beforeSend: function(){

                    $("#divLoading").show();

                },

                //do this regardless if it was successful or not

                complete: function (data) {

                    $("#divLoading").hide();

                }

            });

        } else {

            alert("Not valid.");

        }

     }

  1. Post:378

    Points:2694
    Re: Loading image not working

    $.post({

            type: 'POST',

            url: url,

            data: data,

            success: function (data) {

                $("#divLoading").hide();

            }

        }).always(function() { $("#divLoading").hide(); });


      Modified On Sep-24-2014 11:58:19 PM
  1. Post:135

    Points:947
    Re: Loading image not working

    thanks

Answer