Click here to Skip to main content
15,880,405 members
Articles / Programming Languages / Javascript
Tip/Trick

Implementation of a loading image using jQuery in MVC

Rate me:
Please Sign up or sign in to vote.
4.72/5 (13 votes)
29 Jul 2013CPOL 46.4K   15   8
This is an implementation of a loading image using jQuery in MVC.

Introduction

This tip is for loading image using jquery in MVC. In this tip, we try to add a loading image on clicking the button before redirect to destination page.

We have to follow the below steps:

Step 1

Add a button to the page:

HTML
<p>
    <a href="../AddUser/Register">
        <input type="button" id="btn" value="Register" /></a>
</p>

Step 2

Add an image with style=”display:none” because initially, it is hidden.

HTML
<img id="loading" style="display: none;" 
    alt="Updating ..." src="../../Content/imgage.gif" />

Step 3

Add a Jquery script within script tag either in head section or in end of page.

JavaScript
<script type="text/javascript">
    $(document).ready(
        function () {
            $("#btn").click(

                function () {
                    $("#loading").show();
                    debugger;
                    $.get('<%= Url.Action("../AddUser/Register") %>', {},
                        function (data) {
                            $('#result').html(data);
                            $('#loading').hide();
                        });
                }
            );
        }
    );
</script>

Note

  • In the url.Action(), we have to provide its controller/action or action if it redirects to the same controller.
  • result is div tag id in which destination page is embedded.
  • loading is an id of image.
  • btn is the id of button on clicking which the processing image appears.

Step 4

We have to specify the below code in destination (httpGet) Action:

C#
Thread.Sleep(2000);

We have to specify this because it holds processing for specified milliseconds.

The result of this processing image is shown below:

On clicking the register button, it shows processing image before redirect to register page.

Image 1

This is my initial tip. I hope this will help you.

Thanks!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionParameter Pin
Siberis17-Jul-13 23:46
Siberis17-Jul-13 23:46 
AnswerRe: Parameter Pin
Antariksh Verma18-Jul-13 1:20
professionalAntariksh Verma18-Jul-13 1:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.