LOADING IMAGE NOT WORKING

Sumit Kesarwani

Total Post:378

Points:2694
Posted by  Sumit Kesarwani
 1110  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. Chintoo Semi

    Post:135

    Points:947
    Re: Loading image not working

    try this:

    form.validate();

    if (form.valid()) {


  1. Ankit Singh

    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. Sumit Kesarwani

    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. Chintoo Semi

    Post:135

    Points:947
    Re: Loading image not working

    thanks

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!