我在MVC中有一个页面,我想每2秒自动重新加载一次,而不需要刷新,我看到了一些例子使用Ajax,但都是使用按钮或在POST-GET事件中引用的。我只想重新加载而不刷新,我能做些什么来执行该功能?谢谢
这是我的观点
@model MVC_PCMonitoring_Dash.Models.TablesPCModels
@{
ViewBag.Title = "Home Page";
}
<!doctype html>
<html lang="en">
<head>
<title>Dashboard</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="~/Content/css/style.css">
</head>
<body>
<div class="wrapper d-flex align-items-stretch">
<nav id="sidebar">
<div class="custom-menu">
</div>
<div class="img bg-wrap text-center py-4" style="background-color: #A40033;">
<div class="user-logo">
<div class="img" style="background-image: url(Images/logo.jpg)"></div>
<h3>TOTAL PC</h3>
</div>
</div>
<ul class="list-unstyled components mb-5">
<li class="active">
<a href="/Home"> Home</a>
</li>
<li>
<a href="/Monitor_BA"> PC 1</a>
</li>
<li>
<a href="/Monitor_BB"> PC 2</a>
</li>
<li>
<a href="#">Exit</a>
</li>
</ul>
</nav>
<!-- Page Content -->
<div id="content" class="p-4 p-md-5 pt-5">
<p align="right">@DateTime.Now</p>
<center>
<img src="~/Images/welcome.png" alt="centered image" />
</center>
</div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/popper.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/main.js"></script>
</body>
</html>
在这种情况下,您应该使用定时器和ajax
setInterval(function(){ --ajax call to refresh---},2000) //2000 means 2 min