views:

40

answers:

1

In my mvc application, i'm having a controller where many actions are their.

I'm having a property for the controller class.

In index controller i'm setting the value for the property ,

will it able to get same value in another action..

public class HomeController : BaseController
    {
int sample =0;

public ActionResult Index(int query)
        {
     this.sample = test;
     }

     public ActionResult Result()
        {
     this.sample  -------- can this 'll give the value of wat i get in index action.

 }

}

+1  A: 

Since the controller will be created and destroyed with each web request, you can't store data in private variables across web requests, which is a good thing because, different users will be making different requests, so you need to use caching.

Try this:

  public class HomeController : BaseController
  {

      public ActionResult Index(int query)
      {
          ControllerContext.HttpContext.Session["query"] = query;
      }

      public ActionResult Result()
      {
          int query = (int)ControllerContext.HttpContext.Session["query"];
      }
  }
Josh Pearce
i have used tempdata will it be common to all user or for single connection seperate tempdata will be there..?
santose
The TempData Data Dictionary is available for the current request and the immediately next request for a single user.
Josh Pearce
instead of using cache you can store it in HttpContext.session["query"], is this a better option ? .. thanks
Mahesh Velaga
Yes, Session is just a user-specific cache, so no need to reinvent it.
Craig Stuntz
I got turned off to Session a while back, but I have no rational reason for being so. Thanks for reminding me, I edited the answer.
Josh Pearce