This question is locked. New answers and comments are not allowed.
Hi. Tell me please, how do not update the data in GridAction? For example on sorting, filtering, etc. When I click on sort, the grid goes to the controller where I need to get data.
7 Answers, 1 is accepted
0
Taras
Top achievements
Rank 1
answered on 10 Feb 2012, 05:52 AM
Nobody can help me?
0
Taras
Top achievements
Rank 1
answered on 24 Feb 2012, 12:34 PM
Up
0
Dadv
Top achievements
Rank 1
answered on 24 Feb 2012, 02:03 PM
Hi,
Can you give more details please?
Can you give more details please?
0
Taras
Top achievements
Rank 1
answered on 24 Feb 2012, 05:36 PM
For example, I have controller:
GetAccounts() - get data from database.
viewModel.Data - DataSource for Grid. When I click on sort, Index()
is called again, and again called GetAccounts().
Each time data is rettrieved from database.
How should I store the data, so as not to get from database each time?
P.S.: Sorry for my english.
public class AccountsController : Controller
{
public ActionResult Index() { AccountCommonViewModel viewModel = new AccountCommonViewModel() { Data = accounts.GetAccounts().ToList(), Filter = new AccountSelectViewModel() }; return View(viewModel); }
}GetAccounts() - get data from database.
viewModel.Data - DataSource for Grid. When I click on sort, Index()
is called again, and again called GetAccounts().
Each time data is rettrieved from database.
How should I store the data, so as not to get from database each time?
P.S.: Sorry for my english.
0
Dadv
Top achievements
Rank 1
answered on 27 Feb 2012, 10:15 AM
Hi,
Do you want a "per session" cached (each client have a specific cache) or a "per application cache" (all clients share the same cache) ?
Session cache :
Application cache :
2 way for that, using an static class (not really the more efficient way) or using output cache attribute (build - in functionality)
For the output cache a good post here : http://dotnetslackers.com/articles/aspnet/Cache-Exploration-in-ASP-NET-MVC-3-Part-1.aspx#s3-detailing-into-the-outputcache-filter
For the static class simply create a class with static properties :
public static class InternalCache
{
private static DateTime LastCached;
private static Dictionary<string, IEnumerable> values = new Dictionary<string,IEnumerable>();
internal static Dictionary<string, IEnumerable> Values
{
get
{
return values;
}
set
{
values = value;
LastCached = DateTime.Now;
}
}
}
Do you want a "per session" cached (each client have a specific cache) or a "per application cache" (all clients share the same cache) ?
Session cache :
public class AccountsController : Controller
{
public ActionResult Index() {
if(Session["Accounts"] == null)
Session["Accounts"] = accounts.GetAccounts().ToList() ;
AccountCommonViewModel viewModel = new AccountCommonViewModel() { Data = Session["Accounts"], Filter = new AccountSelectViewModel() }; return View(viewModel); }
}Application cache :
2 way for that, using an static class (not really the more efficient way) or using output cache attribute (build - in functionality)
For the output cache a good post here : http://dotnetslackers.com/articles/aspnet/Cache-Exploration-in-ASP-NET-MVC-3-Part-1.aspx#s3-detailing-into-the-outputcache-filter
For the static class simply create a class with static properties :
public static class InternalCache
{
private static DateTime LastCached;
private static Dictionary<string, IEnumerable> values = new Dictionary<string,IEnumerable>();
internal static Dictionary<string, IEnumerable> Values
{
get
{
return values;
}
set
{
values = value;
LastCached = DateTime.Now;
}
}
}
public class AccountsController : Controller
{
public ActionResult Index() {
if(!InternalCache.Values.ContainsKey("Accounts"))
InternalCache.Values["Accounts"] = accounts.GetAccounts().ToList() ;
AccountCommonViewModel viewModel = new AccountCommonViewModel() { Data = InternalCache.Values["Accounts"], Filter = new AccountSelectViewModel() }; return View(viewModel); }
}0
Taras
Top achievements
Rank 1
answered on 27 Feb 2012, 10:42 AM
Yes, I need cache per session. Thanks a lot.
One more question: How should I clear Session["Accounts"] when I close view?
One more question: How should I clear Session["Accounts"] when I close view?
0
Dadv
Top achievements
Rank 1
answered on 27 Feb 2012, 11:08 AM
You can't clean it when you "close the view" because you will loose all the magic of the session.
All you can do is to clear it when an other specific view is call.
for example calling "View1" => save Session data : Session["Accounts"] = accounts.GetAccounts().ToList() ;
calling "View2" (the home view for example) => clear session data : Session["Accounts"] = null;
then when you call an other time the view1 after being on the view2, the data are retrieve in the database.
An other option is to use a parameter for that, look to my answer here : http://www.telerik.com/community/forums/aspnet-mvc/grid/grid---refresh-and-paging-button-url-customization.aspx
Regards,
All you can do is to clear it when an other specific view is call.
for example calling "View1" => save Session data : Session["Accounts"] = accounts.GetAccounts().ToList() ;
calling "View2" (the home view for example) => clear session data : Session["Accounts"] = null;
then when you call an other time the view1 after being on the view2, the data are retrieve in the database.
An other option is to use a parameter for that, look to my answer here : http://www.telerik.com/community/forums/aspnet-mvc/grid/grid---refresh-and-paging-button-url-customization.aspx
Regards,