title: ASP.NET MVC updating the View from my Controller via a time interval event
body: <p>I have been working with my ASP.NET application and made some significant changes to it. When I first started out I wanted to know how do I update my view from the controller. Bit with the thanks of the incredible people who guided me I was able to learn about Ajax and how to send update data via client. I did this by hitting a refresh button I created.</p>
  
  <p>But now I want the data to be updated after a certain amount of time has passed (3 minutes, as an example). How do I go about in setting something like this up? Do I set that up inside the Ajax.BeginForm function or is it done via JavaScript? I would like to know your input on this. </p>
  
  <p><strong>My Model:</strong></p>
  
  <pre><code>using System.Collections.Generic;
  
  namespace Portfolio.Models
  {
      public class HomeViewModel
      {
          public List&lt;User&gt; User = new List&lt;User&gt;();
      }
  }
  </code></pre>
  
  <p><strong>My Controller:</strong></p>
  
  <pre><code>using Portfolio.Models;
  using System;
  using System.Timers;
  using System.Web.Mvc;
  
  namespace Portfolio.Controllers
  {
      public class HomeController : Controller
      {
          public HomeViewModel model = new HomeViewModel();
          Timer Timer = new Timer();
  
          public ActionResult Index()
          {
              ModelState.Clear();
  
              model.User.Add(new User() { Name = "A", Email = "a@email.com", Date = DateTime.Now.AddHours(-1) });
              model.User.Add(new User() { Name = "B", Email = "b@email.com", Date = DateTime.Now.AddHours(-2) });
              model.User.Add(new User() { Name = "C", Email = "c@email.com", Date = DateTime.Now.AddHours(-3) });
  
              if (Request.IsAjaxRequest())
              {
                 return PartialView("_FooView", model);
              }
          }            
      }
  }
  </code></pre>
  
  <p><strong>My View:</strong></p>
  
  <pre><code>    @{
          ViewBag.Title = "Home Page";
      }
      @model Portfolio.Models.HomeViewModel
  
   @using (Ajax.BeginForm(
      new AjaxOptions
      {
        HttpMethod = "get",
        InsertionMode = InsertionMode.Replace,
        UpdateTargetId = "FooView"
      }))
    {
      &lt;button type:"submit" value:"Refresh/&gt;
  
    }
  @Html.Partial("_FooView", Model)
  </code></pre>
  
  <p><strong>My newly created Partial View:</strong></p>
  
  <pre><code>@model Portfolio.Models.HomeViewModel
  &lt;div id="FooView"&gt;
          @for (int i = 0; i &lt; Model.User.Count; i++)
          {
              &lt;form&gt;
                  &lt;div id="Name"&gt;@Model.User[i].Name&lt;/div&gt;
                  &lt;div id="Email"&gt;@Model.User[i].Email&lt;/div&gt;
                  &lt;div id="Date"&gt;@Model.User[i].Date&lt;/div&gt;
              &lt;/form&gt;
              &lt;div&gt;---------------------
          &lt;/div&gt;
          }    
      &lt;/div&gt;
  </code></pre>