ViewBag没有刷新

时间:2019-06-02 11:45:14

标签: c# asp.net-mvc

在控制器中,我有一些每10秒更改一次的值,我将此值传递给ViewBag。问题是视图的值永远不会改变,页面加载时它仅显示第一个值。

这是我的代码:

public class LonLat
{
    public int Lon { get; set; }
    public int Lat { get; set; }
}

public ActionResult DrawPath()
{
   ViewBag.Message = "Your application description page.";
   Response.AddHeader("Refresh", "5");
   SetTimer();
   lonLat = new LonLat
   {
       Lon = 200,
       Lat = 100
   };
   ViewBag.Message = lonLat;
   return View();
 }

private void SetTimer()
{
  aTimer = new System.Timers.Timer(5000);           
  aTimer.Elapsed += OnTimedEvent;
  aTimer.AutoReset = true;
  aTimer.Enabled = true;
}

private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
   lonLat.Lon = new Random().Next(-180, 180);
   lonLat.Lat = new Random().Next(-90, 90);
   ViewBag.Message = lonLat;           
}

在视野中:

@{
    var data = ViewBag.Message;
}

<h3>RecordName: @data.Lon</h3>
<h3>RecordDetail: @data.Lat</h3>

但是我alawaws看到值200和100,如何使ViewBag刷新?

我尝试添加Response.AddHeader("Refresh", "5");

0 个答案:

没有答案