0

I want that a user of my application to enable or disable the viewing of a certain page for other users. For that i need , somehow an application variable, a bool to be set true or false whether the user decides to enable or disable the view for other users.

The functionality that i want is : when i click on a button to disable the view for a page(for other users that are connected to site), and when i press back to enable it .

I can achieve this with the use of a database, by changing the value of a field in a table with true or false.But this approach is ineffective and not elegant.

Can someone tell me how I can achieve this without using databases ? Is there any application variable / session variable / or cookie to achieve this ? Is there something that i should write in Global.asax ?

I use ASP.NET MVC 4..

Please help!

Siva Gopal
  • 3,474
  • 1
  • 25
  • 22
Stefan P.
  • 331
  • 6
  • 23
  • Can you please specify, what trial(s) you did were not yielding expected result? Did you try with caching to reduce the database trips? – Siva Gopal Nov 26 '14 at 11:29
  • i did store the value in a database, with one table and one field with values true or false. I don't want to create a table with one field in my database for this functionality. I'm trying to find another way to solve this problem... A more elegant one.. Mine is kinda hardcoded.. – Stefan P. Nov 26 '14 at 11:33
  • why did i get downvote ? i think it's a legitimate question ... – Stefan P. Nov 26 '14 at 11:37
  • If you want to persist the value, your going to have to store it somewhere, whether its a database or an xml file. Session is no good because that's per user (another user could not access it) –  Nov 26 '14 at 11:40
  • Cookies are per use as well. How would another user know your cookie values or vice versa? –  Nov 26 '14 at 12:07
  • what do you mean when you say cookies are per use ? You meant per user ? – Stefan P. Nov 26 '14 at 12:17
  • I solved it. I stored the data into an xml and it's much more elegant.. – Stefan P. Nov 26 '14 at 13:15

1 Answers1

-1

In you Controller declare "static variable" say

static bool disableViewPage;

Set this value on click of button in the some action.

Example :

public class SomeViewController : Controller
{
   [HttpPost]
   public ActionResult DisableView(bool value)// on button click
   {
      SecondController.DisableViewPage=value;
      return View("<SomeView>")
   }

}

public class SecondController : Controller
{
   public static bool DisableViewPage;
   // your views

}
SeeTheC
  • 1,560
  • 12
  • 14
  • the problem is that the controller i have the button is different from the controller i have the page that will be disabled... – Stefan P. Nov 26 '14 at 11:25
  • You can access that varible using the class name. as it is static and public. – SeeTheC Nov 26 '14 at 11:30
  • @SeeTheC The question is for an elegant approach and its not a good practice to assign controller properties etc since instantiation & Dependency Injection(if used) etc., would get messed up. – Siva Gopal Nov 26 '14 at 11:31
  • I can't save the values in static variable because I need persistent data. I need the page to be disabled even when i close my session. – Stefan P. Nov 26 '14 at 11:35
  • In one of my applicant I uses Db and static class object. I need lot of value to cached at local side . So I did is , on start of application, I fetched all values from DB and stored in this static object for avoiding multiple DB fetch . When ever there is change in values I just update my object and also Db – SeeTheC Nov 26 '14 at 11:45