0

Problem: I want to monitor incoming data from a subscription without having to refresh the page.

Using RazorPages.

At the minute I have a very simple app:

(code behind)

public class PageModel
{ 
     public List<string> dataList = new List<string>();

     public void OnGet()
     {
        var mySubscriber = Subscriber.subscribe(actionMethod);
     }

    private async Task actionMethod(string data)
    {
          dataList.Add(data);
    }
}

(page)

@foreach(var s in Model.dataList){
  <div>@s</div>
}

The subscriber calls the actionMethod whenever a new string is received.

However currently, this is static and once the page loads, I have to refresh the page to get more data.

How to refactor this so that the page automatically updates with new data?

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
Mr Giggles
  • 2,483
  • 3
  • 22
  • 35
  • razor is an HTML generator from templates, so you can use html and js freely there. use long polling requests using JS `setInterval` and ajax requests to get the data – CME64 Mar 13 '19 at 14:34

2 Answers2

0

You can't do it without refresh, becouse Razor tec isn't bidirectional. In asp.net (or core) u can use SignalR.

Link to MSDN manual

0

You have to make some AJAX calls periodically on the UI to update your view. As @Denis mentioned in his answer, SignalR might be a solution, but this is more for notifications. In your case, it seems to me that you will have to perform this call once in a while to refresh some data that you display.

If there is just a portion of your display that you want to update, you could use a Partial View. Here is a thread on this matter.

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76