【发布时间】:2015-12-03 06:53:22
【问题描述】:
我正在开发一个 MVC 应用程序网站并遇到了问题,我在 HomeController 上编写了一个名为“AlertCheckFunction”的函数,该函数应该检查某些条件并将消息添加到字符串列表中,然后将此列表添加到ViewBag.Message 并在 HTML 视图中输出。我正在尝试编写一个在计时器上设置的 Jquery 函数,以便这些警报每隔一定秒数重新检查一次条件,并将这些重新输出到我在 jquery 中的自动代码。
第 138-138 行 是我的自动代码,当它将文本放入 html 中的无序列表时效果很好,但是当我使用 Razor 调用 viewbag 时,我无法得到任何输出. 第 141-16 行2 是我设置计时器查询函数然后调用我的 c# 函数的所有不同尝试,我得到了一个警报框来处理计时器。
但我不知道如何正确调用该函数。 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>
家庭控制器
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>
【问题讨论】:
-
公共 Actionresult 是我的家庭控制器上代码的开始,底部的 div 是我在 html 中调用它的地方。任何帮助以及最简单和最平凡的解决方案都将不胜感激,它在决赛周的 1:30 和我的批判性思维能力现在就要投篮了,所以现在很难理解任何事情了
-
请正确格式化您的代码。
标签: javascript c# jquery model-view-controller