ASP.NET MVC 3 LoadingElementDuration not working?

2019-07-17 19:51发布

问题:

I am playing with a small MVC 3 application and I have set the LoadingElementDuration parameter on my ajax-submitted form but it seems that it has no effect at all. If I set it to 1 or 5000, the animation is always played at the same speed.

Is it a known bug or something?

Here is my view code, it is partially taken from "Pro ASP.NET MVC 3 Framework"; the animation doesn't last for 5 seconds:

@model IEnumerable<MvcAjax.Models.Appointment>

@{
    ViewBag.Title = "Appointment List";
}

<h2>Appointment List</h2>

@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "tabledata", LoadingElementId = "loading", LoadingElementDuration = 5000 }))
{ 
    <p id="loading" style="font-size: 70px; background-color: Red; display: none; overflow: hidden;">Loading data from server...</p>

    <table>
        <thead>
            <tr>
                <th>Client Name</th>
                <th>Appointment Date</th>
            </tr>
        </thead>
        <tbody id="tabledata">
            @Html.Partial("AppointmentData", Model)
        </tbody>
    </table>

    <div>
        @Html.DropDownList("id", new SelectList(new[] { "All", "Joe", "Jane", "Bob" }, ViewBag.SelectedValue ?? "All"))
        <input type="submit" value="Submit" />
    </div>

}

回答1:

I tweaked a bit the library code and it seems that it is really a bug.
The problem is in the following line of code from jquery.unobtrusive-ajax.js:

duration = element.getAttribute("data-ajax-loading-duration") || 0;

If I change it to:

duration = parseInt(element.getAttribute("data-ajax-loading-duration")) || 0;

The animation plays correctly. When it's a number, the duration argument in the jQuery show function is supposed to be an integer, not a string.

They probably should change this in the official library. I hope this can help other people.