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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
    • ¥20 腾讯企业邮箱邮件可以恢复么
    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
    • ¥15 让node服务器有自动加载文件的功能
    • ¥15 jmeter脚本回放有的是对的有的是错的
    • ¥15 r语言蛋白组学相关问题