Question

I started learning ASP.NET MVC few days ago and I'm just curious what features MVC offers as far as updating the view goes. I have a lot experience with WPF, which has the magical INotifyPropertyChanged interface that updates the UI component the property is bound to. As far as I know, there is no such interface that we can implement in the Model that would then update the View asynchronously without refreshing the page.

So my question is, what are the best practices for updating the view from the model without having to refresh the page? I know in traditional ASP.NET we would use AJAX to make that happen, but I'm wondering if MVC offers a cleaner, more ".NETy" way of achieving that.

So for example in my scenario, I have a WebGrid that is bound to a list in the model and I have a timer that refresh the data in the model every few seconds. What would be the best way to push these changes to the view? Is AJAX the only option?

Was it helpful?

Solution

In short, to achieve behavior similar to what you're describing would require ajax of some sort. The connection between the model referenced in the controller action methods and the UI is broken with every request/response cycle, so realtime updates of the model properties don't happen as you're used to in WPF.

The cleanest way to do this would be to create a SPA using something along the lines of AngularJS and use MVC's WebAPI feature to expose an API consumed by the client. This won't get you realtime property updates out of the box, but a good framework will help you organize your client code and avoid page refreshes if that is your design goal.

OTHER TIPS

You can create actions on your controller that return a JsonResult. Hit that action via ajax and update your client with the returned data. Like @maf748 suggested, I'd look into web api if most of your app should behave this way.

I don't know of a more ".NETy" way. If you return a View, you're going to refresh the page so you need to return just plan ol' json or xml and handle displaying the update on the client side.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top