Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
    }

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?
Posted

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900