0

My application has kendo dropdown list to show states. The state drop downlist has been used on multiple views.

@(Html.Kendo().DropDownListFor(m => m.BlankReturn.StateProvinceCode)
                .DataTextField("StateName")
                .DataValueField("StateCode")
                .DataSource(source =>
                {
                    source.Read(read =>
                    {
                        read.Action("GetStateProvinces", "Lookup");
                    });
                })                
            )

Right now every instance of this dropdownlist makes a call to server to get states. I want kendo to load states from the server only on the first call but any subsequent call needs to get it from client cache. How do i configure this?

Update1
Few answers below suggest to use server side caching. I was looking for client side caching. For example jQuery's ajax method will cache GET method result (unless you explicitly disabled caching). I am assuming Kendo is using jQuery to make server call internally. However i guess kendo is disabling ajax caching. So kendo makes the server call each time to get result. My question is how do i enable client side caching so there wont be any server call after first call.

LP13
  • 30,567
  • 53
  • 217
  • 400
  • Take a look at http://stackoverflow.com/questions/8205637/asp-net-mvc3-iis7-5-cache-control-maxage-is-always-0-not-good-for-client-side or http://stackoverflow.com/questions/22443932/cache-control-no-store-must-revalidate-not-sent-to-client-browser-in-iis7-as. I think it is something that IIS is doing, not kendo. – The Dread Pirate Stephen Oct 26 '16 at 20:19
  • 1
    Use [OutputCacheAttribute] if you are not using any 3rd party APIs for Cache. – Vijai Oct 27 '16 at 01:13

1 Answers1

0

The answer is not Kendo related. If you are using MVC then you can either load the state collection from System.Web.Cache or use the .net MVC caching method attributes.

Using Global Cache - The controller will always be called, however, if you cache the States on load then there will be a reduced number of database roundtrips.

using System.Web;
public ActionResult GetStates()
{
    List<State> states=(List<State>)System.Web.HttpContext.Current.Cache["MY_STATES"];
    if(states==null)
    {
        states=myDataLayer.LoadStates();

        System.Web.HttpContext.Current.Cache.Insert(
              "MY_STATES", 
               states, 
               null, 
               System.Web.Caching.Cache.NoAbsoluteExpiration, 
               new TimeSpan(0,1440,0), 
               System.Web.Caching.CacheItemPriority.Normal, 
               null);         
    }
    return states;
}

MVC method Attributes - Set the browser cache attributes and instruct the browser via headers to cache for a specific time. There will be no controller calls while the associated browser cache has not yet expired.

[OutputCache(Duration = 86400)]
public ActionResult GetStates()
{
    return myDataLayer.LoadStates();
}

MVC method Attributes - Single Config - Set the browser cache attributes

[OutputCache(CacheProfile = "TypeTableCacheProfile")]
public ActionResult GetStates()
{
    return myDataLayer.LoadStates();
}

along with the config

  <system.web>
    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <clear />
          <!-- 24 hours-->
          <add varyByParam="*" duration="86400" name="TypeTableCacheProfile" />          
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>
 </system.web>
Ross Bush
  • 14,648
  • 2
  • 32
  • 55
  • The OutputCacheAttribute should trigger client-side caching. It essentially tells the browser on the first request "cache like this", then on subsequent requests, the client will follow the "instructions" provided by the first request and won't make the request(if so directed by the first request caching instructions). – The Dread Pirate Stephen Oct 27 '16 at 15:26