Prevent Refresh on the page from generating additional queries to the BD MVC C#

Asked

Viewed 38 times

0

This is the case. My Actionresult does a heavy database query and returns randomized data by business definition.

The user can refresh the screen. If you update the page, you lose what was done and the return of the query is another.

Does anyone know how to prevent running the link again or refreshing my Actionresult from running again?

  • Complementing. If I control via the session that the query has already been made, the return object of this Actionresult will be null. because it is initiated in this Actionresult obviously.

  • I don’t understand your question. It would be nice if you put part of the code to better understand yourself.

  • and if you save the result of the query to Session, and check, if it has resulted in Session returns it, if it does not execute the query ?!

1 answer

0

When you do the search for the first time, store it in a Session or Viewbag, something that says that the search has already been done, then when the user updates the page, you can check the Reload event via javascript. Example with Viewbag:

Actionresult

ViewBag.Executou = 1;

View

<script type="text/javascript">
    window.onbeforeunload = function() {
        if(@ViewBag.Executou == 1){     
            return false;
        }
    }
</script>

Thus the page will be executed only once.

Note: this event does not work in Opera.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.