Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • David W. 159 posts 284 karma points c-trib
    Nov 24, 2014 @ 11:01
    David W.
    0

    CachedPartial with querystring

    I'm trying to use @Umbraco.CahcedPartial to present a image gallery. I would like the cahce to take the querystring of the page into account so that /Mypage?year=2014 return a diffrent cacheresult than /Mypage?year=2013.

    Been trying this in my template

    @Html.CachedPartial("MediaGallery", null, 3600, false, false, new ViewDataDictionary { { "year", Request.QueryString["year"] } })

    But that results in a YSOD saying 

    The model item passed into the dictionary is of type 'System.Web.HttpValueCollection', but this dictionary requires a model item of type 'Umbraco.Web.Models.RenderModel'.

    Any ideas?

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Nov 24, 2014 @ 13:01
    Dave Woestenborghs
    100

    What you are trying will not work. If you look at the documentation of the cached partial :

    http://our.umbraco.org/documentation/Reference/Mvc/partial-views at the bottom.

    You can create a marco partial and that will call your partial view. Then you creat a macro that renderds your macro partial view. You can define a year parameter on the macro and pass the year querystring to that. Macro's will be cached on parameter values.

    You can find documentation on macro partials here  : http://our.umbraco.org/documentation/reference/templating/macros/Partial-View-Macros/

    Dave

     

  • Jeremy Pyne 106 posts 246 karma points MVP c-trib
    Jun 21, 2016 @ 21:21
    Jeremy Pyne
    2

    This will actually work the way you want, you have to pass an additional parameter generating a unique due passed of the parameters that affect the layout. IE either of these.

    @Html.CachedPartial("MediaGallery", Model, 3600, false, false, new ViewDataDictionary { { "year", Request.QueryString["year"] } }, (model, viewData) => viewData["year"].ToString())
    

    Or this will take a list of viewData Key's and attach them all.

    @Html.CachedPartial("MediaGallery", Model, 3600, false, false, new ViewDataDictionary { { "year", Request.QueryString["year"] } }, (model, viewData) => String.Join("", new string[] {"year"}.Select(s => viewData[s].ToString()) ) )
    
  • Jeremy Pyne 106 posts 246 karma points MVP c-trib
    Jun 21, 2016 @ 21:37
    Jeremy Pyne
    1

    Another cleaner option is to add a extension method in your site code somewhere.

    public static Func<object, ViewDataDictionary, string> CacheBy(this HtmlHelper htmlHelper, params string[] keys)
    {
        return (model, viewData) => String.Join("", keys.Select(s => viewData[s].ToString()));
    }
    

    Then call it like so:

    @Html.CachedPartial("MediaGallery", Model, 3600, true, false, new ViewDataDictionary { { "year", Request.QueryString["year"] } },CacheBy("year", "anotherParameter") )
    
Please Sign in or register to post replies

Write your reply to:

Draft