【问题标题】:Auto refresh in ASP.NET MVCASP.NET MVC 中的自动刷新
【发布时间】:2017-02-04 22:26:58
【问题描述】:

在网络表单中我会这样做

    <script type="text/JavaScript">
    function timedRefresh(timeoutPeriod) {
        setTimeout("location.reload(true);", timeoutPeriod);
    }
    </script>

    <body onload="JavaScript:timedRefresh(5000);">

或代码隐藏 Page_Load

Response.AddHeader("Refresh", "5");

问题如何在ASP.NET MVC3中让屏幕每5秒刷新一次

【问题讨论】:

  • 这两种方法在 MVC 中的工作方式与在 WebForms 中的工作方式完全相同...... :)

标签: javascript asp.net-mvc asp.net-mvc-3


【解决方案1】:

你可以在 MVC 中做同样的事情:

<script type="text/javascript">
function timedRefresh(timeoutPeriod) {
    setTimeout(function() {
        location.reload(true);
    }, timeoutPeriod);
}
</script>
<body onload="JavaScript:timedRefresh(5000);">
    ...
</body>

或使用元标记:

<head>
    <title></title>
    <meta http-equiv="refresh" content="5" />
</head>
<body>
    ...
</body>

或在您的控制器操作中:

public ActionResult Index()
{
    Response.AddHeader("Refresh", "5");
    return View();
}

【讨论】:

  • 如果我只想刷新一次视图怎么办?
  • @ISeeSharp 您必须使用 JQuery 实现自己的代码。
猜你喜欢
  • 1970-01-01
  • 2020-08-09
  • 2011-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-22
  • 1970-01-01
相关资源
最近更新 更多