title: MVC 5 Auto refreshing partial view
body: <p>I have a partial view which is displayed as the Footer of the site in _Layout. The controller calculates a value every second - the number of victims of computer crime. </p>
  
  <p>Controller Code:</p>
  
  <pre><code>namespace EUWebRole.Controllers
  {
  public partial class PartialFooterStatisticsController : Controller
      {
  
      [OutputCache(NoStore = true, Location = OutputCacheLocation.Client, Duration = 1)]
      public ActionResult Index()
          {
          // 14 victims per second.
          // what second is it
          int hrs = DateTime.Now.Hour;
          int min = DateTime.Now.Minute;
          int sec = DateTime.Now.Second;
  
          int totalSeconds = sec + (min * 60) + (hrs * 3600);
          int totalVictims = totalSeconds * 14;
          ViewData["TotalVictims"] = totalVictims.ToString("N0");
  
          return PartialView("_PartialFooterStatistics");
          }
      }
  }
  </code></pre>
  
  <p>The Partial view looks like this - no formatting or anything - I just want the value for now :P</p>
  
  <pre><code>@ViewData["TotalVictims"]
  </code></pre>
  
  <p>Finally, the _Layout section for this is here:</p>
  
  <pre><code>  @Html.Partial("_PartialFooterStatistics")
  </code></pre>
  
  <p>How can I get the value to display in _Layout and update each second without refreshing the whole page - I am guessing that is what the Partial view will prevent but not sure.</p>
  
  <p>Sorry for such a simple question but we all start somewhere.</p>
  
  <p>Thanks</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>Ok I am a little further forward now.
  The GetVictimCount is being called only once.
  <img src="https://i.stack.imgur.com/yF4MJ.png" alt="Developer tools output"></p>
  
  <p>Here is my current Controller code:</p>
  
  <pre><code> public partial class PartialFooterStatisticsController : Controller
  {
  
  
      public ActionResult Index()
      {
          return PartialView("_PartialFooterStatistics");
  
      }
  
      public JsonResult GetVictimCount()
      {
  
          int hrs = DateTime.Now.Hour;
          int min = DateTime.Now.Minute;
          int sec = DateTime.Now.Second;
  
          int totalSeconds = sec + (min * 60) + (hrs * 3600);
          int totalVictims = totalSeconds * 14;
  
          //logic here for getting your count
          var victimCount = totalVictims;
  
          return Json(victimCount, JsonRequestBehavior.AllowGet);
      }
  
  }
  </code></pre>
  
  <p>My Partial View code:</p>
  
  <pre><code>&lt;script src="~/Scripts/jquery-1.10.2.js"&gt;&lt;/script&gt;
  &lt;div id="victimCountId"&gt;
      Result
  &lt;/div&gt;
  
  
  &lt;script type="text/javascript"&gt;
  
  var timeoutId;
  $(document).ready(function () {
      timeoutId = window.setTimeout(UpdateCount, 1000);
  });
  
  function UpdateCount() {
      $.getJSON("PartialFooterStatistics/GetVictimCount", function (dataResponse) {
          var div = $('#victimCountId').html(dataResponse);
  
          printResult(div, item);
  
      });
  }
  
  function printResult(div, item) {
      div.append("Result " + item.Line1);
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>and my _Layout Code</p>
  
  <pre><code>     &lt;footer&gt;
          &lt;p&gt;&amp;copy; @DateTime.Now.Year - My ASP.NET Application&lt;/p&gt;
          @Html.Partial("_PartialFooterStatistics")
      &lt;/footer&gt;
  &lt;/div&gt;
  
  
  
  @Scripts.Render("~/bundles/jquery")
  @Scripts.Render("~/bundles/bootstrap")
  @RenderSection("scripts", required: false)
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  
  &lt;script type="text/javascript"&gt;
  
  $(function () {
  
      setInterval(function () { $('#_PartialFooterStatistics').load('GetVictimStatistics'); }, 1000); // every 3 sec
  
  });
  
  &lt;/script&gt;
  
  function printResult(div, item) {
      div.append("Result " + item.Line1);
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>As said the Partial view is not being called each second, and the output isn't right either... I am expecting "Result " and a number formatted like 100,000 which it is in the controller but instead all I get is 100000.</p>