Armen Mkrtchyan
Armen Mkrtchyan

Reputation: 921

Use same model data in controller's actions

I have a controller and two actions.

In my Products action I am filling my collection. And then when I call Index action, ProductManager.ReadProducts() method called again.

I don't want to connect to the database on second action call.

public class ProductController : Controller
{
    public List<Product> ProductsList = ProductManager.ReadProducts();

    public ActionResult Index(long Id)
    {
        return View(ProductsList.FirstOrDefault(x => x.Id == Id));
    }

    public ActionResult Products()
    {
        return PartialView(ProductsList);
    }
}

Upvotes: 0

Views: 82

Answers (2)

adricadar
adricadar

Reputation: 10219

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);
    }
}

Upvotes: 4

Igor Semin
Igor Semin

Reputation: 2496

Use one of the shared container, for example Session

public class ProductController : Controller
{
    public ActionResult Index(long Id)
    {
        return View(GetProductList().FirstOrDefault(x => x.Id == Id));
    }

    public ActionResult Products()
    {
        return PartialView(GetProductList());
    }

    private IList<Product> GetProductList()
    {
        var productList = Session["ProductsList"];

        if (productList != null)
        {
            return productList;
        }

        productList = ProductManager.ReadProducts();
        Session["ProductsList"] = productList;

        return productList;
    }
}

Upvotes: 2

Related Questions