Home > DeveloperSection > Forums > Loading image not working
Sumit Kesarwani

Total Post:378

Points:2694
Posted on    September-20-2014 12:45 AM

 ASP.NET MVC .NET  ASP.NET MVC 
Ratings:


 4 Reply(s)
 746  View(s)
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!



Chintoo Semi

Total Post:135

Points:947
Posted on    September-20-2014 12:47 AM

try this:

form.validate();

if (form.valid()) {



Ankit Singh

Total Post:341

Points:2389
Supported
Posted on    September-20-2014 12:48 AM

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.");

    }

 }


Sumit Kesarwani

Total Post:378

Points:2694
Posted on    September-20-2014 12:49 AM

$.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

Chintoo Semi

Total Post:135

Points:947
Posted on    September-24-2014 12:07 AM

thanks

Don't want to miss updates? Please click the below button!

Follow MindStick