1

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Slethron
  • 143
  • 1
  • 4
  • 11

1 Answers1

9

Alright, you asked for a suggestion, so I'll try and remain high-level.

First, you'll want to implement two views:

  1. Index - This is your view that's returned by your Index ActionResult functon on your controller.
  2. Stats - This is a partial view included in your Index view as such:

    <div id="refreshme">
        @Html.Partial("_Stats", Model.Stats)
    </div>
    

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:

[HttpGet]
public ActionResult Stats(...)
{
    //...
    return View("_Stats", Model);
}

Now, for the JS side:

function refresh() {
    $.get('/index/post', function(result) {
        $('#refreshme').html(result);
    });
}
setInterval(refresh, 3000);

So the objectives are as such:

  1. Strip out your part that you want refreshed from the rest of the page and put it in a partial view.
  2. Add a controller action method that renders just that partial view.
  3. Include that partial view on your Index view with a container wrapped around it so it can be easily updated.
  4. Add a javascript function that'll get the latest view rendering from the controller and overwrite the current stats on the page.

Hopefully this will get you going in the right direction.

Adam Terlson
  • 12,610
  • 4
  • 42
  • 63
  • Thanks for the reply. I have stripped out that part and put it in a partial view called stats. But that Render function is not recognized in my index view file. I've googled it but can't seem to find how to make the program recognize it. – Slethron Aug 15 '11 at 13:23
  • I'm sorry, I was writing blind and meant to use `@Html.Partial`. My mistake. http://rachelappel.com/razor/partial-views-in-asp-net-mvc-3-w-the-razor-view-engine/ – Adam Terlson Aug 15 '11 at 13:38
  • Thanks, but now it doesn't recognize that Model parameter in this line: return View("_Stats", Model); – Slethron Aug 15 '11 at 13:45
  • Is your view strongly typed? If you don't know what I mean by Model, I strongly suggest that you read up on the fundamentals of MVC before proceeding (Model is the 'M' in 'MVC'). There's some underlying knowledge required to implement a website using the MVC pattern and my answer. Microsoft's website of course has some great resources. http://www.asp.net/mvc/tutorials/asp-net-mvc-overview-cs Put simply, Models are passed to your Views by the Controller. You need to define in your View what kind of Model is getting passed in (strongly-typed or dynamic). – Adam Terlson Aug 15 '11 at 13:52