Click here to Skip to main content
15,119,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
 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


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.
Posted
Updated 18-Jul-18 0:26am
v2
Comments
F-ES Sitecore 22-Jun-18 10:01am
   
You'll need to do this with ajax. Have some code in a js file that is on every page that uses window.setInterval to call a js function at your chosen interval. In the function you call make an ajax call to the controller and the controller can either return a raw number and you then update the relevant element on the page to show that new number, or similarly it can return a view so you'll get an html segment back and you can update the relevant part of the screen with that html. If the data coming back from the controller is complex then the view might be easier, if it is just a simple number then have it return just the number.
JoshWigley 22-Jun-18 10:06am
   
I'm currently using a very simple piece of JS, which at the moment is just refreshing the webpage (Which is horrid). I've taken a look at doing an Ajax call, but will need to do a bit more reading to fully understand how to implement the Ajax call. I'm seeing a lot to do with GET/POST etc. As I'm not trying to insert any data, I assume that I'm wanting to use the GET method, rather than POST?

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.[^]
   
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.

C#
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);
 }
   
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
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900