weixin_33727510 2020-04-12 22:19 采纳率: 0%
浏览 68

进度条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.

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)