在控制器的动作中使用相同的模型数据 [英] Use same model data in controller's actions

查看:139
本文介绍了在控制器的动作中使用相同的模型数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器和两个动作。

在我的产品行动,我填我的收藏。然后再次调用时,我称之为首页操作, ProductManager.ReadProducts()方法。

我不希望连接到第二个动作调用数据库。

 公共类ProductController的:控制器
{
    公开名单<产品与GT;产品列表= ProductManager.ReadProducts();    公众的ActionResult指数(长编号)
    {
        返回查看(ProductsList.FirstOrDefault(X => x.Id ==编号));
    }    公众的ActionResult产品()
    {
        返回PartialView(产品列表);
    }
}


解决方案

作为@斯蒂芬 - muecke提到你有几个解决方案

解决方案1: 对话 的,但请记住, 会话的是每个用户。如果你的产品有特定的用户可以使用它。

 公共类ProductController的:控制器
{
    公开名单<产品与GT;产品列表{
            获得{
                VAR产品=(会话[产品列表]作为名单<产品与GT;);
                如果(产品== NULL)
                {
                    产品= ProductManager.ReadProducts();
                    会话[产品列表] =产品;
                }                回报的产品;
            }
        }    //动作
}

解决方案2: 缓存 的是为所有用户,如果你的产品是针对所有用户,你在一个地方存储所有的人。

 公共类ProductController的:控制器
{
    公开名单<产品与GT;产品列表{
            获得{
                VAR产品=(HttpRuntime.Cache [产品列表]作为名单<产品与GT;);
                如果(产品== NULL)
                {
                    产品= ProductManager.ReadProducts();
                    HttpRuntime.Cache [产品列表] =产品;
                }                回报的产品;
            }
        }    //动作
}

解决方案3::您可以使用的 的OutputCache 的缓存动作的输出。

 公共类ProductController的:控制器
{
    公开名单<产品与GT;产品列表= ProductManager.ReadProducts();    [的OutputCache(持续时间= 60的VaryByParam =*)]
    公众的ActionResult指数(长编号)
    {
        返回查看(ProductsList.FirstOrDefault(X => x.Id ==编号));
    }    [的OutputCache(持续时间= 60的VaryByParam =无)]
    公众的ActionResult产品()
    {
        返回PartialView(产品列表);
    }
}

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

解决方案

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

这篇关于在控制器的动作中使用相同的模型数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆