Loader Page

MNH ASP 101 Reputation points
2024-06-06T11:40:54.26+00:00

Respected All,

please see below image i want to put loading page before Mvc Actionresult return view.

so can u please suggest me how to do it ??

User's image

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,410 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 27,656 Reputation points
    2024-06-06T12:12:41.9933333+00:00

    please see below image i want to put loading page before Mvc Actionresult return view. so can u please suggest me how to do it ??

    If I understand correctly you want to display an a loading notification while the browser is waiting for a response. Use JavaScript to toggle the display property which shows or hides a "Loading..." message or gif. The action response refreshes the page which either resets the HTML content (this example) or replaces the content if a redirect is done.

            [HttpGet]
            public IActionResult Index()
            {
                return View();
            }
            [HttpPost]
            public IActionResult Index(string submit)
            {
                Thread.Sleep(5000);
                return View();
            }
    
    

    Index View

    @{
        ViewData["Title"] = "Index";
    }
    
    <h1>Index</h1>
    
    <form id="form" method="post">
        <button type="submit" name="submit" class="btn btn-primary">Submit</button>
    </form>
    <div id="loading" style="display:none">
        Loading...
    </div>
    
    @section scripts {
        <script>
            const form = document.getElementById("form");
            form.addEventListener("submit", showLoading);
    
            function showLoading() {
                document.getElementById("loading").style.display = "block";
            }
        </script>
    }
    
    
    

    Also, your flow is not technically correct. The processing within the action and before returning an MVC ActionResult unless a JavaScript application is handling the processing


1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,181 Reputation points
    2024-06-06T16:11:33.5+00:00

    displaying a loader page is a separate request. there are two common approaches.

    1. the requesting page uses javascript to display a loading message before any page requests (links, submit). above example.
    2. the requested action returns the loading page. this loading page has a hidden form with the postback data, and does an auto post via javascript to the action that will perform the actual processing. the processing action has a different name, or uses a form value to know its not the splash screen.
    0 comments No comments