0

I'm working on a project where I have to keep data that is up to date. I've been working on using a Timer to keep the data that my controller has synced with my DB every x seconds. My problem is that I can't get my View to Update/Refresh/Reload when the data is updated. I don't have to get the page to update without clicking anything on the page.

Index method

public ActionResult Index()
    {
        if (aTimer.Enabled == false)
        {
            initTimer();
        }
        var ordersLinks = db.OrdersLinks.Include(o => o.List).Include(s => s.Order_Stop_Link).Include(i => i.OrdersInfoes);
        return View(ordersLinks.ToList());
    }

Timer Event Timer is a System.Timers.Timer

public void TimerEvent(object sender, ElapsedEventArgs e)
    {
        var OldDB = db.OrdersLinks.FirstOrDefault().OrderNr;

        foreach (var entity in db.ChangeTracker.Entries())
        {
            entity.Reload();
        }
        var NewDB = db.OrdersLinks.FirstOrDefault().OrderNr;

        if (OldDB != NewDB)
        {
            Index(); //This calls into the Index Method but doesnt update the view
            //RedirectToAction("Index");
        }
    }

SOLVED I used SignalR thanks to the comments, special thanks to @Stephen Muecke.

Jemomi_DK
  • 41
  • 7
  • Hi @Jemomi_DK , you can refer to [this](http://stackoverflow.com/questions/5396282/auto-refresh-in-asp-net-mvc). Hope it helps. :-) – Shawn Yan Jun 17 '16 at 07:37
  • 4
    There is no point putting a timer in the server method. It would need to be in the view (using javascript timer to call a method using ajax which returns a partial view so you can update the DOM). But you should consider using SignalR to push changes to the client. –  Jun 17 '16 at 07:38
  • Your indexmethod returns a View. But you arent doing anyting with this result. Please provide some more code and let us see what your View-Method does. – lokusking Jun 17 '16 at 07:39
  • @lokusking the return View will should pass back the data list to a View call "Index". So I don't see that is the problem. – Shawn Yan Jun 17 '16 at 07:42
  • Thanks alot for all the comments! after more trial and error i have desided to try out SignalR since it sounds like it can do everything i need. special thanks to @Stephen Muecke. I will update the post if/when i get something working. – Jemomi_DK Jun 17 '16 at 11:47

0 Answers0