As @stephen-muecke mentioned you have few solutions
Solution 1: Session but keep in mind that Session is per user. If your products are user specific you can use it.
public class ProductController : Controller
{
public List<Product> ProductsList {
get {
var products = (Session["ProductsList"] as List<Product>);
if(products == null)
{
products = ProductManager.ReadProducts();
Session["ProductsList"] = products;
}
return products;
}
}
// actions
}
Solution 2: Caching which is for all users, if your products are for all users you store in a single place for all of them.
public class ProductController : Controller
{
public List<Product> ProductsList {
get {
var products = (HttpRuntime.Cache["ProductsList"] as List<Product>);
if(products == null)
{
products = ProductManager.ReadProducts();
HttpRuntime.Cache["ProductsList"] = products;
}
return products;
}
}
// actions
}
Solution 3: You can make use of OutputCache to cache the output of actions.
public class ProductController : Controller
{
public List<Product> ProductsList = ProductManager.ReadProducts();
[OutputCache(Duration=60, VaryByParam="*")]
public ActionResult Index(long Id)
{
return View(ProductsList.FirstOrDefault(x => x.Id == Id));
}
[OutputCache(Duration=60, VaryByParam="none")]
public ActionResult Products()
{
return PartialView(ProductsList);
}
}