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.
我正在开发一个MVC应用程序网站并且遇到了障碍,我在我的HomeController上编写了一个名为“AlertCheckFunction”的函数,它应该检查某些条件然后将消息添加到字符串列表然后将此列表添加到ViewBag.Message这是在HTML视图中输出的。我正在尝试编写一个在计时器上设置的Jquery函数,以便这些警报将每隔一定的秒数重新检查标准,并将它们重新输出到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.
第138-138行是我的自动自动收报机,当它将文本放在html中的无序列表中时效果很好,但是当我用Razor调用viewbag时,我无法输出任何内容。第141-162行是我设置计时器查询功能然后调用我的c#函数的不同尝试,我已经有一个警报框来处理计时器。
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>
2 个解决方案
#1
0
I think you are mixing up a bunch of Javascript and MVC code. You cannot call the method AlertCheckFunction
from Javascript if it is located in your HomeController.
我认为你混合了一堆Javascript和MVC代码。如果它位于HomeController中,则无法从Javascript调用方法AlertCheckFunction。
What you can do is, ensure that the method is available as a Controller Action and accessible on a URL like /Home/AlertCheckFunction
.
您可以做的是,确保该方法可用作控制器操作,并可在/ Home / AlertCheckFunction等URL*问。
Your attempt to a $.get
is the closest to the answer. Check if the function and its response (preferably in JSON) is available at a URL and make the call in the following pattern.
你尝试$ .get是最接近答案的。检查函数及其响应(最好是JSON)是否在URL中可用,并按以下模式进行调用。
setInterval(function () {
$.get('/Home/AlertCheckFunction', function (resp) {
console.log(resp); // This will be the JSON response.
});
}, 3000);
You might also want to pass any parameters that you want to check in that function. Parameters can be passed as a query string. More info here Use querystring variables in MVC controller
您可能还希望传递要在该功能中检查的任何参数。参数可以作为查询字符串传递。更多信息在MVC控制器中使用查询字符串变量
#2
0
Instead of putting your items to the ViewBag in the controller, try returning a JSON string with the messages you want to put in your alert box. Then your ticker can do an ajax call to your controller and get the new alerts.
不要将项目放到控制器中的ViewBag中,而是尝试返回一个JSON字符串,其中包含要放入警报框的消息。然后你的自动收报机可以对你的控制器进行ajax调用并获得新的警报。
Alternatively you can look into something like angular, knockout or ember that will change your DOM for you when the data behind changes - if you structure it right.
或者,您可以查看角度,挖空或余烬之类的东西,当后面的数据发生变化时,它会为您更改DOM - 如果您正确构建它。
In your controller:
在你的控制器中:
public JsonResult GetAlerts()
{
return Json(Alerts, JsonRequestBehavior.AllowGet);
}
And in your view:
在你看来:
$.ajax({
url: '@Url.Action("GetAlerts")',
// data: {'alertType': 1}, if you wanted to send some data. The parameter name will have to be _exactly_ the same
type: 'GET',
success: function (response) {
$("#alertsDiv").html(response);
}
});
#1
0
I think you are mixing up a bunch of Javascript and MVC code. You cannot call the method AlertCheckFunction
from Javascript if it is located in your HomeController.
我认为你混合了一堆Javascript和MVC代码。如果它位于HomeController中,则无法从Javascript调用方法AlertCheckFunction。
What you can do is, ensure that the method is available as a Controller Action and accessible on a URL like /Home/AlertCheckFunction
.
您可以做的是,确保该方法可用作控制器操作,并可在/ Home / AlertCheckFunction等URL*问。
Your attempt to a $.get
is the closest to the answer. Check if the function and its response (preferably in JSON) is available at a URL and make the call in the following pattern.
你尝试$ .get是最接近答案的。检查函数及其响应(最好是JSON)是否在URL中可用,并按以下模式进行调用。
setInterval(function () {
$.get('/Home/AlertCheckFunction', function (resp) {
console.log(resp); // This will be the JSON response.
});
}, 3000);
You might also want to pass any parameters that you want to check in that function. Parameters can be passed as a query string. More info here Use querystring variables in MVC controller
您可能还希望传递要在该功能中检查的任何参数。参数可以作为查询字符串传递。更多信息在MVC控制器中使用查询字符串变量
#2
0
Instead of putting your items to the ViewBag in the controller, try returning a JSON string with the messages you want to put in your alert box. Then your ticker can do an ajax call to your controller and get the new alerts.
不要将项目放到控制器中的ViewBag中,而是尝试返回一个JSON字符串,其中包含要放入警报框的消息。然后你的自动收报机可以对你的控制器进行ajax调用并获得新的警报。
Alternatively you can look into something like angular, knockout or ember that will change your DOM for you when the data behind changes - if you structure it right.
或者,您可以查看角度,挖空或余烬之类的东西,当后面的数据发生变化时,它会为您更改DOM - 如果您正确构建它。
In your controller:
在你的控制器中:
public JsonResult GetAlerts()
{
return Json(Alerts, JsonRequestBehavior.AllowGet);
}
And in your view:
在你看来:
$.ajax({
url: '@Url.Action("GetAlerts")',
// data: {'alertType': 1}, if you wanted to send some data. The parameter name will have to be _exactly_ the same
type: 'GET',
success: function (response) {
$("#alertsDiv").html(response);
}
});