I have encountered an issue with my code involving 2 ajax API calls. One call fetches data through a lengthy process, while the other retrieves progress values using the IProgress interface and runs every 5 seconds. The value from ReportProgress
successfully updates and is assigned to the _generateRecipeCardsStatus
property. However, when I call getProgress()
-> GeneratePrintRecipeCardsStatus
, the _generateRecipeCardsStatus
property always remains zero. Can anyone help me identify where I may be going wrong?
<script>
getData();
getProgress()
// long process
function getData() {
const token = $('input[name="__RequestVerificationToken"]').val();
$.ajax({
headers: {
'X-Requested-With': 'XMLHttpRequest',
"X-ANTI-FORGERY-TOKEN": token
},
type: 'POST',
url: `@Url.Action("GeneratePrintRecipeCards", "MenuPrintout")?id=` + @Model.RecipeCardLabelId + `&menuHeaderId=` + @Model.MenuHeaderId,
success: function(response){
console.log(response)
const { errorMsg, progressValue } = response
inProgressEle.hide();
close.show();
if (errorMsg) {
toast(errorMsg, "error")
}
else if (progressValue > 0) {
console.log("progress value:" + progressValue);
}
else{
expand.hide()
collapse.hide()
statusEle.text("Ready");
completedEle.fadeIn()
}
}
});
}
// get progress
function getProgress() {
setInterval(function(){
$.ajax({
url: '@Url.Action("GeneratePrintRecipeCardsStatus", "MenuPrintout")',
type: "GET",
success: function (response) {
console.log(response)
$('#lblStatus').text(response)
}
})
}, 5000)
}
</script>
// controller
public class MenuPrintoutController : BaseController
{
private readonly IMenuLabelService _menuLabelService;
private int _generateRecipeCardsStatus;
private Progress<int> _progress;
public MenuPrintoutController(IMenuLabelService menuLabelServicee)
{
_menuLabelService = menuLabelService;
}
// Is always zero
public int GeneratePrintRecipeCardsStatus()
{
return _generateRecipeCardsStatus;
}
// Updates fine
private void ReportProgress(int progress)
{
_generateRecipeCardsStatus = progress;
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> GeneratePrintRecipeCards(int id, int menuHeaderId)
{
try
{
_progress = new Progress<int>(ReportProgress);
var data = await _menuLabelService.DoAsync(int menuHeaderId, _progress);
TempData.Put("PrintRecipeCards", data);
return Json(new { Success = true });
}
catch (Exception exp)
{
return Json(new { ErrorMsg = exp.Message });
}
}
}
// service
public interface IMenuLabelService
{
Task<object> DoAsync(int menuHeaderId, IProgress<int> progress);
}
public class MenuLabelService : IMenuLabelService
{
public async Task<object> DoAsync(int menuHeaderId, IProgress<int> progress)
{
var menuFoodItemsList = _context.FoodItemUnits
.Where(x => x.MenuSectionFoodItemUnits.Any(y => y.Menusection.MenuheaderId == menuHeaderId))
.Select(x => x.Fooditem)
.ToList();
var menuFoodItems = new List<PrintLabel>();
var donePointer = 0;
foreach (var menuFoodItem in menuFoodItemsList)
{
menuFoodItems.Add(menuFoodItem);
// ...
progress.Report((int)(++donePointer / (double)menuFoodItemsList.Count * 100));
donePointer++;
}
return menuFoodItems;
}
}