Ответ 1
Правильный и простой способ сделать это с помощью SignalR. Загрузите Microsoft SignalR в https://www.nuget.org/packages/Microsoft.AspNet.SignalR/2.1.2
Создайте класс концентратора в отдельной папке в пути проекта, называемой хабами, добавьте два файла класса в папку хабов
Startup.cs
using Owin;
using Microsoft.Owin;
[assembly: OwinStartup(typeof(SignalRChat.Startup))]
namespace SignalRChat
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Any connection or hub wire up and configuration should go here
app.MapSignalR();
}
}
}
ProgressHub.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace RealTimeProgressBar
{
public class ProgressHub : Hub
{
public string msg = "Initializing and Preparing...";
public int count = 1;
public static void SendMessage(string msg , int count)
{
var message = "Process completed for " + msg;
var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>();
hubContext.Clients.All.sendMessage(string.Format(message), count);
}
public void GetCountAndMessage()
{
Clients.Caller.sendMessage(string.Format(msg), count);
}
}
}
В контроллере
// assemblies
using Microsoft.AspNet.SignalR;
using RealTimeProgressBar;
//call this method inside your working action
ProgressHub.SendMessage("initializing and preparing", 2);
В представлении
<!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
<!--Reference the SignalR library. -->
<script src="~/Scripts/jquery.signalR-2.1.2.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="~/signalr/hubs"></script>
<!--SignalR script to update the chat page and send messages.-->
<script type="text/javascript">
$(document).ready(function () {
$("#progressBar").kendoProgressBar({
min: 0,
max: 100,
type: "percent",
});
});
function StartInvoicing()
{
var progressNotifier = $.connection.progressHub;
// client-side sendMessage function that will be called from the server-side
progressNotifier.client.sendMessage = function (message, count) {
// update progress
UpdateProgress(message, count);
//alert(message);
};
// establish the connection to the server and start server-side operation
$.connection.hub.start().done(function () {
// call the method CallLongOperation defined in the Hub
progressNotifier.server.getCountAndMessage();
});
}
// Update the progress bar
function UpdateProgress(message, count) {
var result = $("#result");
result.html(message);
$("#progressBar").data("kendoProgressBar").value(count);
}
</script>
Подробнее см. некоторые существующие статьи с помощью google