如何使用计时器调用MVC控制器方法 [英] How to call a MVC controller method using a timer

查看:145
本文介绍了如何使用计时器调用MVC控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我目前正在学习MVC,并希望获得一些帮助,让我的项目能够运作。我目前正在编写一个仪表板系统,用于检查多个文件夹中的文件数量并返回值。我想重新运行执行此操作的代码(在Controller内)每X秒数/分钟等。



我不希望网页刷新,我只是想运行该方法,以便网页上的数字正确更新。



因为我正在使用MVC,我很遗憾没有使用System.Windows.Forms.Timer功能,本来是完美的。



这是我的控制器的代码:



Hi everyone!

I'm currently learning MVC and would like some assistance in getting a piece of my project to work. I'm currently writing a dashboard system which checks the number of files, within a number of folders and returns the value. I would like to re-run the code that does this (within the Controller) every X amount of seconds/minutes etc.

I don't want the webpage to be refreshed, I just want to run the method so that the numbers on the webpage update correctly.

As I'm using MVC, I unfortunately do not have use of the System.Windows.Forms.Timer functionality, that would have been perfect for this.

Here is the code for my controller:

 public ActionResult Documentation()
        {


            try
            {
                #region clinic
                System.IO.DirectoryInfo letters= new System.IO.DirectoryInfo(@"\\TEST\TEST\TEST\TEST\TEST");

                var file = letters.GetFiles().Length;

                ViewBag.letterCount = file.ToString();

                return View();
            }
            catch(exception)
            {
             throw;
            }
}

                #endregion





我尝试了什么:



我试过在C#和Ajax中做这个/ jQuery,但我不确定1)我想要实现的是甚至可能的,2)哪种方法被认为是行业内的最佳实践。任何方向都会非常有帮助。



What I have tried:

I have tried looking at doing this within C# and Ajax/jQuery, but I'm unsure whether 1) What I'm trying to achieve is even possible, 2) Which method would be considered best practise within industry. Any direction would be really helpful.

推荐答案

你可以用jquery和ajax来做,只需谷歌搜索如何定期进行ajax调用。



在jquery中可以使用setTimeout和setInterval等方法,可用于在x分钟后调用控制器操作。然后更新页面内容。



例如见以下帖子:



如何在一定的时间间隔后发送ajax请求。 [ ^ ]
You can do it with jquery and ajax, just do a google search on how to do ajax call periodically.

There are methods avaiable in jquery like setTimeout and setInterval which can be used to call for example the controller action after x minutes. and then update the content of page.

For example see the below post:

How to send ajax request after certain interval of time.[^]


我会按照您的尝试关注AJAX路径,创建一个新的控制器操作并将AJAX设置为关闭客户端计时器。



I would be following the AJAX route as you have tried, creating a new controller action and set the AJAX to fire off of a client side timer.

private string _FolderPath = Server.MapPath("your filepath here");
private System.IO.SearchOption _SrcOpt = System.IO.SearchOption.TopDirectoryOnly;

public JsonResult GetFileCountJSON() {
	int filecount = Directory.GetFiles(_FolderPath, "*", _SrcOpt).Length;
	object ReturnData = new { "FileCount", filecount };
	return Json(ReturnData, JsonRequestBehavior.AllowGet);
 }


大家好,



感谢您的帮助!



我决定一种更简单的方法(或者对我来说效果更好的方法)是在MVC中创建部分视图,然后创建一个脚本来刷新那些局部视图。



感谢所有建议。



谢谢,

Josh
Hi Everyone,

Thanks for your help!

I decided an easier way (Or a way which worked better for me) was to create partial views within MVC, and then create a script to refresh those partial views.

Appreciate all the advice.

Thanks,
Josh


这篇关于如何使用计时器调用MVC控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆