我试图从一个AJAX post响应的结果中的foreach
函数获取原始计数。我面临的问题是,在我的帖子回复中,我收到了所有这些foreach的迭代的总数,而不是个人。这样做的目的是通过在每次迭代中填充进度条来显示上载的进度。如何检索控制器函数的计数到ajax进度条asp.net mvc
控制器:
public JsonResult progressFunction(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 allPocEmailAddresses = new List<InstitutionPointOfContact>();
var pocEmailAddresses = new List<InstitutionPointOfContact>();
//retrieve all Point of contact based upon selected statuses per each loop
var result = new List<InstitutionPointOfContact>();
foreach (var item in status)
{
result = _uow.InstitutionPointOfContactRepository.GetAllByStatus(item).ToList();
allPocEmailAddresses.AddRange(result);
}
// Retrieve the poc email addresses based on the who is intended to receive the email message
if (systemGeneralAnnouncement.SendToRecipients.Contains("(1) All Three POCs"))
{
pocEmailAddresses = allPocEmailAddresses;
}
else
{
if (systemGeneralAnnouncement.SendToRecipients.Contains("(2) All POCs"))
{
pocEmailAddresses.AddRange(allPocEmailAddresses.Where(r => r.PointOfContactType == PointOfContactTypes.Primary).ToList());
}
if (systemGeneralAnnouncement.SendToRecipients.Contains("(3) All Compliance POCs"))
{
pocEmailAddresses.AddRange(allPocEmailAddresses.Where(r => r.PointOfContactType == PointOfContactTypes.Secondary).ToList());
}
if (systemGeneralAnnouncement.SendToRecipients.Contains("(4) All Authorities"))
{
pocEmailAddresses.AddRange(allPocEmailAddresses.Where(r => r.PointOfContactType == PointOfContactTypes.SigningAuthority).ToList());
}
if (systemGeneralAnnouncement.SendToRecipients.Contains("(5) All Rate POCs"))
{
pocEmailAddresses.AddRange(allPocEmailAddresses.Where(r => r.PointOfContactType == PointOfContactTypes.TuitionRates).ToList());
}
if (systemGeneralAnnouncement.SendToRecipients.Contains("(6) Specified Email Address"))
{
var pocs = new List<InstitutionPointOfContact>();
string[] emails = systemGeneralAnnouncement.EmailAddresses.Split(',');
foreach (string email in emails)
{
var addPoc = new InstitutionPointOfContact { Email = email };
User user = _uow.UserRepository.GetByEmail(email);
if (user == null)
{
addPoc.FirstName = "Not Created Account Yet";
}
else
{
addPoc.FirstName = user.FirstName;
addPoc.LastName = user.LastName;
}
List<InstitutionPointOfContact> opeidAssociatedToUser =
_uow.InstitutionPointOfContactRepository
.GetAllPocsByEmail(email)
.ToList();
if (opeidAssociatedToUser.Count == 0)
{
addPoc.IDNumber = "N/A";
}
else
{
string[] idArray = idAssociatedToUser
.Select(x => x.IDNumber)
.ToArray();
addPoc.IDNumber = string.Join(",", opeidArray);
}
pocs.Add(addPoc);
}
pocEmailAddresses.AddRange(pocs);
}
}
ViewBag.UploadProgress = 0;
// if any poc addresses were found...
if (pocEmailAddresses.Count > 0)
{
string emailBody = WebUtility.HtmlDecode(systemGeneralAnnouncement.EmailBody);
foreach (InstitutionPointOfContact emailAddress in pocEmailAddresses.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;
//iterative for progress bar
ViewBag.UploadProgress++;
}
}
return Json (ViewBag.UploadProgress, JsonRequestBehavior.AllowGet);
}
AJAX:
$
(document).ready(function(){
$.ajax({
type: "POST",
url: "progressFunction",
cache: false,
cacheControl: "no-cache",
statusCode: {
500: function() {
errorWhileSavingData()
}
},
success: function (data) {
alert()
GenerateProgressBar(data);
}
});
});
我的问题是,我可以在后功能恢复的个体数从控制器(Viewbag.uploadProgress)等等我可以将它作为进度条中的变量计数传递给它?
更新:为了清楚起见,我需要做的是获得foreach(1〜n)的个人计数,而不是完成的计数,这正是我现在正在接受的计数。
更新2:SingalR是不是真的在这种情况下的选择,因为它是太过分了,这样一个小的过程,一个期望的结果将来自一个“滚你自己的”
你为什么不返回实际的数字? –
如果我正确地理解了这个问题,为什么我不把它作为原始值返回? – jpears
@CristianSzpisjak我没有返回原始值的原因是因为最终用户不一定会理解上传进度,因为它在幕后发生。本质上,我们将选定的元素作为元值(所有POC,合规性,权限),然后将附加到这些值的数据库值传递给计划任务,稍后将其作为电子邮件发送(每周一次),因为我期望做一个进度条是需要3-5秒,我想告诉他们它正在进行并完成。 – jpears