开发者

controller member

开发者 https://www.devze.com 2022-12-12 02:05 出处:网络
In my mvc application, i\'m having a controller where many actions are their. I\'m having a property for the controller class.

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 ge开发者_JAVA技巧t in index action.

    }

}


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"];
      }
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消