I have been searching extensively on how to create a navigation link that will display an ajax Loading icon while the action method is running in MVC 3 using the Razor view engine. While I have found plenty of information regarding adding a loading icon, all of them seem to pertain to posting data within the current view.
I have a simple menu page that has actionlinks to call action methods on other pages, which in turn will render a view. An example to go to the OrdersController and Index action method is displayed below.
Menu.cshtml
@Html.ActionLink("Orders", "Index", "Orders",null,new { id = "OrderLink" })<br />
I am trying to modify the link above in a way that while the Index action is processing and before the view is returned, to show an ajax loading icon.
I have created a div that contains the loading icon and placed it in my shared _Layout.cshtml page.
_Layout.cshtml
<div id="ajaxLoaderDiv" style="position:fixed; top:40%; left:45%; z-index:1234; display:none;">
<img src="@Url.Content("~/Images/AjaxLoaderImg.gif")" alt="Loading..."class="ajax-loader" />
</div>
To try and display the ajax loading icon I tried changing my link to an Ajax ActionLink.
Menu.cshtml
@Ajax.ActionLink("Orders", "Index", "Orders", new AjaxOptions { LoadingElementId="ajaxLoaderDiv" });<br />
This works in the sense that it displays the ajax Loading icon for a few seconds. And I can even put a breakpoint inside the Index action method of the OrdersController and it will hit it. But when the Action Method completes, the Orders\Index.cshtml never renders to the page. I just remain on the Menu\Index.cshtml page.
I know that the Orders/Index.cshtml is supposed to be rendering a view.
OrdersController.cs
public ActionResult Index()
{
....
return View(OrdersViewModel);
}
What am I doing wrong here? Is this view being returned to my ajax call rather than rendering? How can I get this functionality?
If I am going about this in the wrong manner, let me know.
Should I be doing this inside the target page's action method instead of on the link when making the request? Maybe by displaying the loading icon as the first line of code in the Index action method that I am requesting, and hiding it again just before returning the view?
I would maybe try and do this through through a JQuery Show/hide combination?
<script type="text/javascript">
function showLoadingSpinner() {
$("#ajaxLoaderDiv").show();
};
function hideLoadingSpinner() {
$("#ajaxLoaderDiv").hide();
};
</script>
Thanks in advance for and and all help and advice
Chad