I am developing a MVC application website and have hit a snag, I have written a function on my HomeController called "AlertCheckFunction" That is supposed to check for certain criteria then and add messages to a list of strings then add this list to ViewBag.Message and that is outputed in a HTML View. I am trying to write a Jquery function that is set on a timer so that these alerts will recheck criteria every certain number of seconds and it will reoutput these to my automatic ticker in jquery.
Line 138-138 is my automatic ticker, and it works great when it put text in an unordered list in the html, but I can't get anything to output when I call viewbag with Razor. Line 141-162 is all my different attempts at setting up a timer query function then calling my c# function, I have gotten an alert box to work on a timer.
but I cannot figure out how to call the function properly. Link of Problem
$(document).ready(function () {
$('.dropdown-toggle').dropdown();
$('.dropdown-toggle').click(function (e) {
e.stopPropagation();
e.preventDefault();
});
});
function tick() {
$('#ticker li:first').slideUp(function () { $(this).appendTo($('#ticker')).slideDown(); });
}
setInterval(function () { tick() }, 5000);
//window.setInterval(AlertCheckFunction, 5000);
//function AlertCheckFunction() { alert('test'); }
//window.setInterval(function () {
// $('AlertCheckFunction');
//}, 2000);
// function alert() {
// $('AlertCheckFunction')
// }
// setInterval(function () { alert() }, 60000)
//window.setInterval(function() {
//$.get('AlertCheckFunction', function(result) {
//});
//}, 3000);
</script>
HOME CONTROLLER
public ActionResult AlertCheckFunction()
{
List<String> Messages = new List<String>();
foreach (var i in db.Ingredients)
{
if (i.Quantity < i.ReOrderPoint)
{
Messages.Add("The quantity of " + i.IngredientName + "Is less than the ReOrderPoint, Suggest placing another order for this ingredient!");
}
else
{
Messages.Add("No alerts from Ingredeints");
}
}
foreach (var c in db.Customers)
{
if (DateTime.Now == c.Birthday)
{
Messages.Add("It is " + c.Name + "'s" + "Birthday Today!");
}
else
{
Messages.Add("No alerts from Customer!");
}
}
foreach (var i in db.Inventories)
{
if (i.InvQuantity <= 5)
{
Messages.Add("The Inventory of " + i.Name + "Is less than or equal to 5, Consider making new batch");
}
else
{
Messages.Add("No alerts from Inventories");
}
}
//DateTime lastMonth = DateTime.Now.AddMonths(-1);
//DateTime twoMonthsAgo = DateTime.Now.AddMonths(-2);
//var sales = db.Sales.Where(j => j.SaleId).ToList();
// foreach (var x in db.Sales)
// {
// var alerts = db.Sales.Where(x => x.SaleId.Count);
ViewBag.Message = Messages;
return RedirectToAction("Index", "Home");
}
HTML
<div>
<ul id="ticker">
@if (ViewBag.Messages != null)
{
foreach (var v in ViewBag.Message)
{
<li>
@v
</li>
}
}
</ul>
</div>