DanCaparroz
DanCaparroz

Reputation: 820

Best Way to AutoRefresh PartialView in ASP.NET MVC

I'm building an application that will run into a television and it will refresh each X seconds.

Its a fixed time and I wont need any call since my application will get the updated data and simply place for the user into a table.

I have searched and found soo many solutions and I will like to know the best one for my case since it looks quite simple.

As its a TV, I'm going to have a fixed table ( no loops) so I will need to address the values to the specific TDs of the tables. The idea of the object I will send to the view is something like:

List<Fields> ResultList = new List<Fields>() 
                        { new Fields { Field1 = 123, Field2 = "ABC"},
                          new Fields { Field1 = 456, Field2 = "DEF"}};

My partial view will have a table with a fixed number of rows and I will populate them with the data that comes fro mthe view.

<table>
    <tr>
        <td id='row1a'></td>
        <td id='row1b'></td>
    </tr>
    <tr>
        <td id='row2a'></td>
        <td id='row2b'></td>
    </tr>
    <tr>
        <td id='row3a'></td>
        <td id='row3b'></td>
    </tr>
</table>

I need to address Field1 to rowXa and Field2 to rowXb

I never did anything with ajax nor jquery to perform it and thats why I'm completely lost on doing it. My application is already gathering the data, arranjing everything as I want. For now, I need to address the specific fields correctly and have no starting point for that.

Thanks in advance

Upvotes: 0

Views: 464

Answers (1)

Peter Smith
Peter Smith

Reputation: 5548

It feels to me that simplest is to use a 'pull' solution:

  • set a timer in JavaScript
  • Use that to start an Ajax call to your controller that will return a partial view as HTML
  • the partial view is the one that generated the table in the first place
  • replace the table with the returned HTML

Another option is to use the timer to poll for changed values and only update individual rows within the table for changed data.

The choice will depend on the number of rows in the table.

UPDATE

Following on from the information below I would start with the simplest solution and only look at the other options if there is any display or timing issues. By setting the row and cell IDs it would be fairly simple just to update individual cells but that might be over kill.

You can also do a table refresh replacing each row in sequence which might give a more static view.

Upvotes: 1

Related Questions