3

We developing Web Application using mvc4. In many scenario we will get value from user in First Page/View which we need to keep in some varaiable until user reach Final Page/View. He/She may pass 4-5 views to reach final view from first view.

To keep Value in MVC. We had 3 ways.

1. Global Variable - But if i assign value in one action method. the value will be reset in another action method. So we dropped it.

2.Session - But we need to keep more then 5 values in each session. So we dropped it.

3.Static Varibale - Which works like charm. but in Multiple user it caused concurrency issue.

Is any other ways in mvc to keep values? please guide me.

tereško
  • 58,060
  • 25
  • 98
  • 150
Ryder
  • 514
  • 1
  • 7
  • 27
  • 3
    Why is keeping more that 5 values in a session bad? – Tommy Oct 14 '14 at 12:54
  • @Tommy Why I should have session value beyond my scope if I don't really need(except my page). – Vivek Ranjan Oct 14 '14 at 12:57
  • @Tommy: We using session oly to keep user information across web app. but for single contoller/page we thought not to go with session to avoid future confusion. – Ryder Oct 14 '14 at 12:57
  • You shouldn't keep it beyond your scope? Dispose of those values when you are done. Session is made for exactly the scenario you described in my opinion, keeping some values for a user for a little bit of time until you are ready to persist them to some other storage. – Tommy Oct 14 '14 at 13:03
  • 1
    Find this http://stackoverflow.com/questions/14154892/scope-of-static-variable-in-multi-user-asp-net-web-application – Madhavan NR Oct 14 '14 at 13:10
  • @Tommy: Thanks Lot. Session is perfect option in my case. I agree. But is any other option is there? If use session means in IDE inteligence wont show. we need to carefull while using it. We just checking for another option. If no way then finally we plan to use session. – Ryder Oct 14 '14 at 13:23
  • 1
    As far as I know, to persist information across multiple request, you either have to go session or create a table in your db that holds those values (you can update the row for that user as they go). There may be other options such as writing to a file, but that would be slow and probably not a well chosen way to go. There are no other constructs in MVC that will allow you to hold data across multiple request. – Tommy Oct 14 '14 at 13:27
  • @Tommy: Thanks lot dude. We plan to use session. – Ryder Oct 14 '14 at 13:37
  • The elephant in this room are cookies, apparently. But yes, the session is way preferable. – Felix Frank Oct 14 '14 at 14:04

1 Answers1

5

Static variables will persist for the life of application domain, that is why you are seeing the concurrency issues with multiple users.

See: Static Variables and their implications in ASP.Net websites

There shouldn't be any problem in storing five values in a session. You can have List<T> and store that in session. Like:

List<string> someValues = new List<string> {"A","B","C","D", "E",};
HttpContext.Current.Session["userValues"] = someValues;

To retrieve it:

var someValues =  HttpContext.Current.Session["userValues"] as List<string>;
if(someValues != null)
{
 // found
}

The only thing you should consider is the size of data. Sessions are stored at server level for each user, storing too much data could cause a problem, though it depends on your configuration.

You may also see: What is ViewData, ViewBag and TempData? – MVC options for passing data between current and subsequent request

Habib
  • 219,104
  • 29
  • 407
  • 436
  • Static variables are not shared across app domains. Each app domain has its instances of static variables. – Dirk Oct 14 '14 at 12:59
  • @Dirk, I didn't mean that it will be shared among multiple app domains, may be I sounded like that, should edit it. – Habib Oct 14 '14 at 13:01
  • @Habib: thanks lot. But is any other option rather then session? i need to handle session very carefully and need to clear session in one page it if user navigating to other page. – Ryder Oct 14 '14 at 13:05
  • @Ryder, I am not sure about your requirement then, you have `get value from user in First Page/View which we need to keep in some varaiable until user reach Final Page/View.`, As far as I know there is nothing like `ViewState` in MVC, which is for single page in Web Forms. – Habib Oct 14 '14 at 13:08
  • 1
    Hai Guys Please refer this link http://stackoverflow.com/questions/14154892/scope-of-static-variable-in-multi-user-asp-net-web-application – Madhavan NR Oct 14 '14 at 13:09
  • 2
    @Ryder: You have your answer. This is what sessions are for. If you have some arbitrary requirement that restricts the use of sessions, then it's your responsibility as a developer to inform and teach the decision makers so that they relax the requirement. After all, you're supposed to be the expert here. That's why they hired you. Static/global variables will simply not work. You've got to use the session for this. – Chris Pratt Oct 14 '14 at 13:27
  • Thanks lot guy. We plan to use session. We just checked is any other way is there. Thanks onc again – Ryder Oct 14 '14 at 13:39