1

I am creating a MVC website, and one of the things I wanted was a spinning gif when pressing the submit button I have, until the new view loads. Below is my current code that unfortunately doesn't work and I don't know why.

<p>
       @using (Ajax.BeginForm("Index", "Home", FormMethod.Get, new AjaxOptions ()
       {
           UpdateTargetId = "result",
           LoadingElementId = "myLoadingElement"
       }))
       {
           @Html.TextBox("search", null, new { style = "width:500px;" })<input type="submit" value="search" />
       }
  </p>                    

//some more code

<div id="myLoadingElement" style="display: none;">
    <img src="~/photos/image"/>
</div>

Does anyone know what my problem may be? I am pretty new to MVC and this is my first time trying to use AJAX Thanks

James
  • 85
  • 1
  • 5
  • Reddit post which got a response: https://www.reddit.com/r/csharp/comments/4t6yzl/loading_icon_when_pressing_submit/ – Warty Jul 17 '16 at 07:09

1 Answers1

0
  1. LoadingElementId should be pointing directly to the .gif image
  2. Your image src attribute doesn't look right, it should be something like src="~/photos/image/loading.gif"
  3. Finally,for AJAX calls to work properly in MVC you need to add a refrence to three javascript libraries.Please note - the order matters:

    3.1)jquery-1.8.0.js

    3.2)jquery.validate.js

    3.3)jquery.unobtrusive-ajax.js

Complete example below.

Controller:

public class HomeController : Controller
{
    public string Index(string search)
    {
        Thread.Sleep(5000);
        return "Hello " + search;
    }
}

View:

<script src="~/scripts/jquery-1.8.0.js"></script>
<script src="~/scripts/jquery.validate.js"></script>
<script src="~/scripts/jquery.unobtrusive-ajax.js"></script>

@using (Ajax.BeginForm("Index", "Home", null, new AjaxOptions()
        {
            UpdateTargetId = "result",
            LoadingElementId = "myLoadingElement"
        },
            null))
{
    @Html.TextBox("search", null, new { style = "width:500px;" })
    <input type="submit" value="search" />
}

<img id="myLoadingElement" src="~/photos/image/loading.gif" style="display:none;width:70px;height:70px;" />
<div id="result">
</div>

EDIT:

Ajax.BeginForm is used when you want to call a controller action and display the result on the same page.If you want to call a controller action and redirect to a different view when you're done you should use the standard Html.BeginForm and to make the loading .gif appear use jQuery:

<script src="~/scripts/jquery-1.8.0.js"></script>
<script type="text/javascript">
    $(function () {
        $("#myform").submit(function (e) {
            $("#myLoadingElement").show();
        });
    });
</script>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "myform" }))
{
    @Html.TextBox("search", null, new { style = "width:500px;" })
    <input type="submit" value="search" />
}
<img id="myLoadingElement" src="~/photos/image/loading.gif" style="display:none;width:70px;height:70px;" />
Denys Wessels
  • 16,829
  • 14
  • 80
  • 120
  • I tried this and I still couldn't get it to work. My view, however, is slightly more complex and is formatted like this. public ActionResult Index(string search) { //lots of code on which view to generate } – James Jul 17 '16 at 12:55
  • So all of my ajax things that i tried adding are in my view, i did nothing to my controller so this may be the problem. Otherwise I don't know what could be wrong. – James Jul 17 '16 at 13:02
  • Did you add a reference to the three JavaScript files that I mentioned?Did you make sure that those files exist in your application? – Denys Wessels Jul 17 '16 at 13:16
  • Ok, just checked and i was using a newer version of jquery in the scripts folder (1.10.2) and changed it and now when i press my button i see the loading gif pop up, however, now my new view doesn't load. – James Jul 17 '16 at 13:32
  • I stepped through my code and it looks like my view return view statement is getting hit but on the website nothing happens. This was all working before so it must be something to do with the ajax. – James Jul 17 '16 at 13:35
  • Debug your action method in the controller.Step through the code line by line.Try simplifying your action method first to have something simple like I show in my example and get that to work first.Programming requires attention to detail and a lot of trial and error, not to mention research.Do you have a div in your page with the id of result? – Denys Wessels Jul 17 '16 at 13:36
  • So I had the div id at the end but i just added it now to the @using with the updatetargetid="result". This now results in the data being loaded as a partial view in my original view. Sadly this isn't what i want either. – James Jul 17 '16 at 13:44
  • I totally get that a partial view is loaded and that's fine. What I wanted was a page where i would press a button, a partial view of a loading gif would be displayed, and then a new view would come up with the data. Is this possible using the ajax.beginform? – James Jul 17 '16 at 13:50
  • Yes it's possible.You need to learn how to do research - http://stackoverflow.com/questions/9391201/ajax-beginform-that-can-redirect-to-a-new-page – Denys Wessels Jul 17 '16 at 13:57
  • I tried doing this but my controller has some logic that it's passing in the view for database lookups, which I couldn't find a way to implement using Json. Is there just a way to have the ajax loading affect just the loading gif and allow the rest of my application to run normally (just have the controller then load the new view). Thanks for your help, and I'm sorry for the constantly asking questions, I'm new to programming. – James Jul 17 '16 at 15:13
  • Please have a look at my edit.I think it will do exactly what you're looking for – Denys Wessels Jul 17 '16 at 17:42