开发者

controller member

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜