marrrschine
marrrschine

Reputation: 632

Refresh view to show updated property in Razor page

I have a newb question to ASP.NET Core 2 MVC / Razor.

If there is a way, how do I set properties eg. from a controller to be updated inside the Page view immediately?

My IndexModel : PageModel class has a property

public IList<CustomObject> ObjectCollection { get; set; }

The ObjectCollection gets modified somehow and I want to update the ObjectCollection.Count() inside my Page view.

If I debug in I can see the ObjectCollection.Count has a proper value. But the value displayed inside the Page view always remains in the initial state which is some old value.

Do I have to refresh my view inside the Razor page somehow?

Upvotes: 1

Views: 2454

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239260

Requests typically generate from the client, if you want the server to be able send something to the client without first receiving a request, you need to use something like web sockets. In the .NET world, that means you'll need SignalR.

How to set this up, exactly, is beyond the scope of Stack Overflow. However, generally speaking, you'll need to create a SignalR hub server side. Then, client-side, you'll need to subscribe to that hub. When something changes on the server-side, your code will send a message to the hub, which will then go out to all subscribed clients. Once the client receives the message, you'll need further client-side code to enact on, i.e. show a new row in a table, or something.

Upvotes: 4

Related Questions