Activating loading animation by Html.BeginForm submission

前端 未结 2 1179
挽巷
挽巷 2021-02-05 16:42

I want to display loading animation when the user clicks on submit button. Simple gif will do the job. This is my code:

@using (Html.BeginForm(\"SData\",\"Crawl\         


        
2条回答
  •  鱼传尺愫
    2021-02-05 17:08

    Edit

    I mistakenly thought the question concerned the AJAX helper. Here's how you could do it using the HtmlHelper.

    First, add an ID to the form so you can grab it using JQuery:

    @using (Html.BeginForm("SData", "Crawl", FormMethod.Post, new { id = "myform" }))
    {
        // the form
    }
    

    Next, add a Javascript event handler to intercept the form submission and display the loading GIF.

    $("#myform").submit(function(e) {
        $("#myLoadingElement").show();
    });
    

    (Original answer follows...)

    Use the AjaxOptions class to set a LoadingElementId, and the Ajax helper will display that element while waiting for the response from the server:

    @using (Html.BeginForm("SData","Crawl", new AjaxOptions() {
        LoadingElementId="myLoadingElement"
    }))
    {
        // form
    }
    

    Then simply place your gif wherever you want it to display (hide it initially):

    
    

提交回复
热议问题