使用asp.net MVC 3异步自动刷新
我编写了一个简单的程序,用于从 WMI 数据库收集一些信息,例如当前的 CPU 使用情况。 我使用 ActionResult Index 函数在 homecontroller 文件中执行这些操作。然后我返回视图并将这些结果显示在主页上。
现在,我应该使用 JQuery 并每 3 秒刷新一次这些值。我不想重新加载整个页面。但是,这些值是我从 WMI 收集的。
有什么好的和简单的(因为我是 Javascript 新手)建议吗?
I've written a simple program that gathers some information from the WMI database such as the current CPU usage.
I perform these operations in the homecontroller file, with an ActionResult Index function. Then I return the View and display these results on the homepage.
Now, I should use JQuery and refresh these values every 3 seconds. I don't want to reload the whole page. But, just these values that I gather from WMI.
Any good and simple(because I'm a complete newbie in Javascript) suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,你征求了建议,所以我会尽力保持高水平。
首先,您需要实现两个视图:
统计信息 - 这是包含在索引视图中的部分视图,如下所示:
您会注意到我传入了一个包含统计信息对象的模型。该统计对象将传递到您的“统计”视图,该视图将知道如何呈现它。
接下来,您需要在 HomeController 中添加一个新的操作方法,您猜对了,它被称为 Stats!此 ActionResult 将仅呈现 Stats 视图并将其作为 HTML 返回。您还需要在其上设置一个
[HttpGet]
标志,以便它可以接受 get 请求:现在,对于 JS 端:
所以目标是这样的:
希望这能让您朝着正确的方向前进。
Alright, you asked for a suggestion, so I'll try and remain high-level.
First, you'll want to implement two views:
Stats - This is a partial view included in your Index view as such:
You'll note that I passed in a Model that contains a stats object. This stats object gets passed to your "Stats" View which will know how to render it.
Next, you'll want to add a new action method in your HomeController called, you guessed it, Stats! This ActionResult will just render the Stats view and return it as HTML. You'll also want to set a flag of
[HttpGet]
on it so it can accept get requests:Now, for the JS side:
So the objectives are as such:
Hopefully this will get you going in the right direction.