1

In ASP.NET MVC, a field which is declare outside a method, the field is initialize every-time.

There I am giving a demo example.

public class TestController : Controller
{

    private List<object> ListItems; 
    // GET: /Test/Index
    public ActionResult Index(int Id)
    {
        ModelSource _model = new ModelSource(Id);
        ListItems = _model.GetItems();//coming info from Model
        return View();
    }


    // GET: /Test/Demo
    public ActionResult Demo()
    {
        int x  = ListItems.Count;
        //Do Something More
        return View(x);
    }
}

Is It possible for ListItems will be initialize for once for every client as Id parameter will be different on client's request.

Ritwick Dey
  • 18,464
  • 3
  • 24
  • 37

1 Answers1

2

As Patrick Hofman stated you can use Sessions or Cookies Here is an example for both:

Cookies:

 string cookievalue ;
 if ( Request.Cookies["cookie"] != null )
 {
    cookievalue = Request.Cookies["cookie"].ToString();
 }
 else
 {
    Response.Cookies["cookie"].Value = "cookie value";
 }
//For removing cookie use following code

if (Request.Cookies["cookie"] != null)
{
    Response.Cookies["cookie"].Expires = DateTime.Now.AddDays(-1);   
}

Sessions:

HttpContext.Current.Session.Add("Title", "Data"); 

string foo = Session["Title"]; 
Masoud Andalibi
  • 3,168
  • 5
  • 18
  • 44