Global variable query in a controller

Asked

Viewed 1,558 times

4

I have a method in my controller that is called through ajax. It populates a global variable within that controller.

Is there any way I can access this global variable already populated by the first method if I call another ajax method on the same page and controller?

The ultimate goal: This variable would be a large list of data that are generated randomly. So I want in the first generate method. The person sees the data and if she gives ok she goes back to the controller with these same data and does another action with them.

  • Have you ever tried to put on a Tempdata ? I don’t quite understand your context

  • Give more details of what you want to do. Sometimes you may have more than one solution to your case. Example: save the information in the browser’s localStorage, if you use Angular you can use the $rootScope (but the information is lost in the refresh page), etc

  • I was left with the msm doubt when he posted, whether it was angular or not but it is on the server side that he is using and not the client the global variable.

  • Why do you need this global variable? Sometimes your error is in the way you try to solve the problem.

2 answers

3


Cannot do per global variable.

For each request the controller is instantiated, so your global variable will lose the value.

A good practice to do this is through Session state or cookies.

One way to do this as a gambit is to make your global variable like static, but it is not recommended use a mutable global state in a web application.

1

This is quite a gambit. What you can do is install Redis and write this "status" per user to a key outside the ASP.NET MVC application.

Access to this variable is done for this client.

  • I was looking for exactly this . :)

Browser other questions tagged

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