问题描述
大家好!
我目前正在学习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.
推荐答案
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控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!