如何减少对我的数据库的请求? [英] How to reduce request to my db?

查看:74
本文介绍了如何减少对我的数据库的请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有网格视图的局部视图.因此,当用户单击添加按钮时,请求将重定向到unitsController的添加方法,并将其添加到数据库之后,我应该从数据库中重新获取所有数据!有没有一种方法可以防止控制器获取所有数据库记录?以下是我当前的控制器

i have a partial view with a gridview in it. so when user clicks add button request will redirect to Add method of unitsController and after add it to databse i should refetch all data from database! is there a way to prevent controller from get all database records? below is my current controller

public class UnitsController : Controller
{
    TList<Units> model=null;

    public ActionResult UnitsPartial()
    {
        if(model==null)
          model = database.GetAll();
        return PartialView(model);
    }

    [HttpPost]
    public ActionResult Add(Units unit)
    {
        if (ModelState.IsValid)
        {
            database.Save(unit);
            model.Add(unit);          
        }

        return PartialView("UnitsPartial", model);
    }


在最后一行中,我想使用return PartialView("UnitsPartial",model)而不是return database.GetAll()来防止数据库查询,但是在Add方法中模型为null!
我的方法正确与否?以及为什么add方法中的模型为null?


in the last line i want to use return PartialView("UnitsPartial", model) instead return database.GetAll() to prevent a databse query, but model is null in Add method!
is my approach correct or not? and why model is null in add method?

推荐答案

由于ASP.NET MVC以断开连接的方式工作,因此您不会在初始化/上一个请求时将值取回存储控制器,除非您将其存储在会话中.

为了更好地理解MVC请求流,这篇文章提供了最好的说明.

以及如何使用会话或会话较少的控制器,您这篇文章提供了您可以做的一切吃饭.
Since ASP.NET MVC is working in disconnected manner you will not get your value back stored at initialization/previous request to controller unless you store it in session.

For better understanding of MVC Request flow this article has the best description.

And how to use session or session less controller you this article has everything you can eat up.


这篇关于如何减少对我的数据库的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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