C# 跟踪“保存在”中的数据库的进度;“生活”;进度条asp.net mvc

C# 跟踪“保存在”中的数据库的进度;“生活”;进度条asp.net mvc,c#,asp.net,asp.net-mvc,asp.net-ajax,C#,Asp.net,Asp.net Mvc,Asp.net Ajax,我需要在.NET4.5中显示SQL加载的进度。目的是显示正在发生的上传的“实时”流。这样上传信息的人可以看到上传正在工作 控制器方法: private void ProgressUpload(int? SystemGeneralAnnouncementId) { var systemGeneralAnnouncement = (SystemGeneralAnnouncementId == null) ? null : _uow.SystemGeneralAnno

我需要在.NET4.5中显示SQL加载的进度。目的是显示正在发生的上传的“实时”流。这样上传信息的人可以看到上传正在工作

控制器方法:

private void ProgressUpload(int? SystemGeneralAnnouncementId) {

                var systemGeneralAnnouncement = (SystemGeneralAnnouncementId == null) ? null : _uow.SystemGeneralAnnouncementRepository.GetById(SystemGeneralAnnouncementId.Value);
                List<Status> status = new List<Status>();

                if (systemGeneralAnnouncement.Statuses.Length > 0)
                {
                    status.AddRange(systemGeneralAnnouncement.Statuses.Split(',').Select(item => (Status)Enum.Parse(typeof(Status), item)));
                }

                var allEmailAddresses = new List<PointOfContact>();
                var EmailAddresses = new List<PointOfContact>();

                //retrieve all Point of contact based upon selected statuses per each loop
                var result = new List<PointOfContact>();
                foreach (var item in status)
                {
                    result = _uow.PointOfContactRepository.GetAllByStatus(item).ToList();
                    allEmailAddresses.AddRange(result);
                }

                // Retrieve the email addresses based on the who is intended to receive the email message
                if (systemGeneralAnnouncement.SendToRecipients.Contains("(1) All Three Contacts"))
                {
                    EmailAddresses = allEmailAddresses;
                }
                else
                {
                    if (systemGeneralAnnouncement.SendToRecipients.Contains("(2) All first Contacts"))
                    {
                        pocEmailAddresses.AddRange(allEmailAddresses.Where(r => r.PointOfContactType == PointOfContactTypes.Primary).ToList());
                    }
                    if (systemGeneralAnnouncement.SendToRecipients.Contains("(3) All Second Contacts"))
                    {
                        pocEmailAddresses.AddRange(allEmailAddresses.Where(r => r.PointOfContactType == PointOfContactTypes.Secondary).ToList());
                    }
                    if (systemGeneralAnnouncement.SendToRecipients.Contains("(4) All third contacts"))
                    {
                        pocEmailAddresses.AddRange(allEmailAddresses.Where(r => r.PointOfContactType == PointOfContactTypes.SigningAuthority).ToList());
                    }
                    if (systemGeneralAnnouncement.SendToRecipients.Contains("(5) All fourth Contacts"))
                    {
                        pocEmailAddresses.AddRange(allEmailAddresses.Where(r => r.PointOfContactType == PointOfContactTypes.TuitionRates).ToList());
                    }
                    if (systemGeneralAnnouncement.SendToRecipients.Contains("(6) Specified Email Address"))
                    {
                        var pocs = new List<PointOfContact>();

                        string[] emails = systemGeneralAnnouncement.EmailAddresses.Split(',');

                        foreach (string email in emails)
                        {
                            var addPoc = new PointOfContact { Email = email };

                            User user = _uow.UserRepository.GetByEmail(email);

                            if (user == null)
                            {
                                add.FirstName = "Not Created Account Yet";
                            }
                            else
                            {
                                addPoc.FirstName = user.FirstName;
                                addPoc.LastName = user.LastName;
                            }

                            List<PointOfContact> idAssociatedToUser =
                                _uow.PointOfContactRepository
                                    .GetAllPocsByEmail(email)
                                    .ToList();


                            if (idAssociatedToUser.Count == 0)
                            {
                                addPoc.IDNumber = "N/A";
                            }
                            else
                            {
                                string[] idArray = idAssociatedToUser
                                    .Select(x => x.IDNumber)
                                    .ToArray();

                                addPoc.IDNumber = string.Join(",", opeidArray);
                            }
                            pocs.Add(addPoc);
                        }
                        EmailAddresses.AddRange(pocs);
                    }
                }
                // if any poc addresses were found...
                if (EmailAddresses.Count > 0)
                {
                    string emailBody = WebUtility.HtmlDecode(systemGeneralAnnouncement.EmailBody);

                    foreach (PointOfContact emailAddress in EmailAddresses.Where(x => x.Email != "" && x.Email != null).ToList())
                    {
                        string firstName = emailAddress.FirstName == null ? "" : emailAddress.FirstName.Trim();
                        string lastName = emailAddress.LastName == null ? "" : emailAddress.LastName.Trim();
                        string userName = firstName + " " + lastName;

                        //Below I Used SqlCommand vs EF because EF has AutoDetectChangesEnabled and it slows things down when adding to the context. Instead of tuning it by turning it to false or 
                        //configure it to use AddRange, SqlCommand is the best option for speed.
调用Post方法的视图:

@model DODMOU.Model.Entities.SystemGeneralAnnouncement
@{
    ViewBag.Title = "Send Email";
    Layout = "~/Views/Shared/_AdministratorLayout.cshtml";
}
<div class="title">
    <h2>Send Email</h2>
</div>
<hr />
<div class="title">
    <h2>Are you sure you want to Send this Email?</h2>
</div>
<div class="row">
    <div class="col-sm-offset-2 col-sm-9">
        <table class="table table-striped table-hover table-bordered table-responsive">
            <tr>
                <th scope="row" class="col-md-1">@Html.DisplayNameFor(model => model.SendToRecipients) :</th>
                <td class="col-md-8">@Html.DisplayFor(model => model.SendToRecipients)</td>
            </tr>
            <tr>
                <th scope="row">@Html.DisplayNameFor(model => model.Subject) :</th>
                <td>@Html.DisplayFor(model => model.Subject)</td>
            </tr>
            <tr>
                <th scope="row">@Html.DisplayNameFor(model => model.EmailBody) :</th>
                <td class="col-sm-8">@MvcHtmlString.Create(HttpUtility.HtmlDecode(Model.EmailBody))</td>
            </tr>
            <tr>
                <th scope="row">@Html.DisplayNameFor(model => model.Since) :</th>
                <td>@Html.DisplayFor(model => model.Since)</td>
            </tr>
        </table>
    </div>
    <div class="row col-sm-offset-2 col-sm-9 text-right">
        <div class="row col-sm-offset-2 col-sm-9 text-right">
            @using (Html.BeginForm("ConfirmSend", "SystemGeneralAnnouncement", FormMethod.Post))
            {
                @Html.AntiForgeryToken()
                @Html.HiddenFor(model => model.SystemGeneralAnnouncementId)
                <div class="form-actions no-color">
                    <input type="submit" value="Send" class="btn btn-primary" /> |
                    @Html.ActionLink("Back to List", "Index")
                </div>
            }
        </div>

    </div>
</div>
@model.model.Entities.systemgeneralnouncement
@{
ViewBag.Title=“发送电子邮件”;
Layout=“~/Views/Shared/_AdministratorLayout.cshtml”;
}
发送电子邮件

您确定要发送此电子邮件吗? @Html.DisplayNameFor(model=>model.SendToRecipients): @DisplayFor(model=>model.SendToRecipients) @Html.DisplayNameFor(model=>model.Subject): @DisplayFor(model=>model.Subject) @Html.DisplayNameFor(model=>model.EmailBody): @MvcHtmlString.Create(HttpUtility.HtmlDecode(Model.EmailBody)) @Html.DisplayNameFor(model=>model.Since): @DisplayFor(model=>model.Since) @使用(Html.BeginForm(“ConfirmSend”、“systemgeneralnouncement”、FormMethod.Post)) { @Html.AntiForgeryToken() @Html.HiddenFor(model=>model.systemgeneralanonounceId) | @ActionLink(“返回列表”、“索引”) }
接收上载信息的视图:

<div class="col-xs-12" style="text-align: center">
    <p>
        <div class="alert alert-success Loading">
            <h2 class="title">General Announcement is Loading...</h2>
            <br />
        </div>
        The selected general announcements are being uploaded...<br />
        <div class="progress"> <div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;"></div> </div>
        @Html.ActionLink("Return to the System General Announcement page.", "Index", "SystemGeneralAnnouncement")
    </p>
    <div id="success" style="display:none">
        <div class="alert alert-success">
            <h2 class="title">General Announcement Successfully Generated!</h2>
        </div>
        The selected general announcement has been successfully generated and will begin sending to the appropriate recipients within the next few minutes.<br />
        @Html.ActionLink("Return to the System General Announcement page.", "Index", "SystemGeneralAnnouncement")
    </div>
</div>
@section scripts{
    <script>
        $.ajax({
            type: 'POST',
            url: "ProgressUpload, systemGeneralAnnouncement",
            data: {},
            success: function (data) {
                $(".Loading").hide();
                $("#success").css("display","block");
            },
            xhr: function () {
                var xhr = new window.XMLHttpRequest();
                xhr.upload.addEventListener("progress", function (evt) {
                    if (evt.lengthComputable) {
                        var percentComplete = evt.loaded / evt.total;

                    }
                }, false);

                return xhr;
            },

        });
    </script>
    }


一般公告正在加载。。。

正在上载选定的一般公告…
@ActionLink(“返回系统一般公告页面”,“索引”,“系统一般公告”)

已成功生成一般公告! 所选的一般通告已成功生成,并将在几分钟内开始发送给相应的收件人。
@ActionLink(“返回系统一般公告页面”,“索引”,“系统一般公告”) @节脚本{ $.ajax({ 键入:“POST”, url:“ProgressUpload,systemGeneralAnnouncement”, 数据:{}, 成功:功能(数据){ $(“.Loading”).hide(); $(“#success”).css(“显示”、“块”); }, xhr:函数(){ var xhr=new window.XMLHttpRequest(); xhr.upload.addEventListener(“进度”,函数(evt){ if(evt.长度可计算){ var percentComplete=evt.loaded/evt.total; } },假); 返回xhr; }, }); }
如上所述,这里的最终目标是允许用户在上传文件时查看页面上的“实时”进度。成功返回将在100%后显示,以让他们知道上载已完成


我的问题是,我缺少什么来公开要上载的当前值?它应该来自事件侦听器,但不显示上载的任何进度

我建议使用signar向客户端更新进度数据。 假设您的上传数据是
IEnumerable
类型,那么您需要在foreach循环中捕获对DB的每次更新,并使用SignalR将数据更新到客户端

请参阅此链接:

<div class="col-xs-12" style="text-align: center">
    <p>
        <div class="alert alert-success Loading">
            <h2 class="title">General Announcement is Loading...</h2>
            <br />
        </div>
        The selected general announcements are being uploaded...<br />
        <div class="progress"> <div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;"></div> </div>
        @Html.ActionLink("Return to the System General Announcement page.", "Index", "SystemGeneralAnnouncement")
    </p>
    <div id="success" style="display:none">
        <div class="alert alert-success">
            <h2 class="title">General Announcement Successfully Generated!</h2>
        </div>
        The selected general announcement has been successfully generated and will begin sending to the appropriate recipients within the next few minutes.<br />
        @Html.ActionLink("Return to the System General Announcement page.", "Index", "SystemGeneralAnnouncement")
    </div>
</div>
@section scripts{
    <script>
        $.ajax({
            type: 'POST',
            url: "ProgressUpload, systemGeneralAnnouncement",
            data: {},
            success: function (data) {
                $(".Loading").hide();
                $("#success").css("display","block");
            },
            xhr: function () {
                var xhr = new window.XMLHttpRequest();
                xhr.upload.addEventListener("progress", function (evt) {
                    if (evt.lengthComputable) {
                        var percentComplete = evt.loaded / evt.total;

                    }
                }, false);

                return xhr;
            },

        });
    </script>
    }