进度条MVC C#

In my project I need to use a progressbar with percentage for the very long process running. And to display the calculation of the Process to user. I foud this code:

Controller:

    public class HomeController : Controller {
        private static int progress;
        public ActionResult Index() {
            return View();
        }
        [HttpPost]
        public JsonResult Start() {
            int count = 10;
            for (int i = 0; i < count; i++) {
                progress = Convert.ToInt32(Convert.ToDouble(i) / Convert.ToDouble(count) * 100);
                System.Threading.Thread.Sleep(500);
            }
            var result = new { StartResult = "ok" };
            progress = 0;
            return Json(result);
        }
        [HttpPost]
        public JsonResult Progress() {
            var result = new { ProgressResult = progress };
            return Json(result);
        }
    }

Views

@{
    ViewBag.Title = "Index";
}

@Html.DevExpress().Button(settings => {
    settings.Name = "btnStart";
    settings.Text = "Start";
    settings.UseSubmitBehavior = false;
    settings.ClientSideEvents.Click = string.Format("function(s, e) {{ OnButtonClick(s, e, '{0}', '{1}'); }}", Url.Action("Start", "Home", null), Url.Action("Progress", "Home", null));
}).GetHtml()
@Html.DevExpress().ProgressBar(settings => {
    settings.Name = "myProgressBar";
    settings.Width = System.Web.UI.WebControls.Unit.Pixel(200);
    settings.ClientVisible = false;
}).GetHtml()

javaScript

var myTimer;
function OnButtonClick(s, e, startUrl, progressUrl) {
    StartActionOnServer(startUrl);
    StartTimer(progressUrl);
}
function StartActionOnServer(startUrl) {
    $.ajax({
        type: 'POST',
        url: startUrl,
        dataType: 'json'
    }).done(ProgressCallbackComplete).fail(CallbackError);
}
function ProgressCallbackComplete(data) {
    StopTimer();
    alert('Accomplished');
}
function StartTimer(progressUrl) {
    btnStart.SetEnabled(false);
    myProgressBar.SetVisible(true);
    myTimer = setInterval(function () {
        $.ajax({
            type: 'POST',
            url: progressUrl,
            dataType: 'json'
        }).done(TimerCallbackComplete).fail(CallbackError);
    }, 1000);
}
function StopTimer() {
    myProgressBar.SetVisible(false);
    myProgressBar.SetPosition(0);
    btnStart.SetEnabled(true);
    if (myTimer) {
        clearInterval(myTimer);
        myTimer = null;
    }
}
function TimerCallbackComplete(data) {
    myProgressBar.SetPosition(parseInt(data.ProgressResult));
}
function CallbackError() {
    StopTimer();
    alert('Callback error');
}

if I insert this code in a new project the progress bar works correctly but if i put this code in my project it doesn't work. the processor executes the Start() method and after the progress() method.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐