Copied to clipboard

Flag this post as spam?

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


  • Mihail 39 posts 142 karma points
    Dec 21, 2016 @ 10:18
    Mihail
    0

    Cache partial view using Umbraco CachedPartial for different model

    I'm using CachedPartial html helper which caches that partial view.

    @Html.CachedPartial("PartialView", MyModel, 3600, true);
    

    In my view, I have the following situation:

    @Html.CachedPartial("PartialView", MyModel, 3600, true);
    
    @Html.CachedPartial("AnotherPartialView", MyModel1, 3600, true);
    
    @Html.CachedPartial("PartialView", MyModel3, 3600, true); // I want to reuse partial view
    

    Seems that first and third view are identically because of CachedPartial ...

    How to make cached partial by model parameter ?

    I tried to use

    @Html.CachedPartial("PartialView", MyModel, 3600, true, false, new ViewDataDictionary(MyModel3));
    

    but same thing.

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Dec 21, 2016 @ 12:38
    Dan Diplo
    2

    You could try using the contextual key builder to create a unique key based on your model. Something like:

    @Html.CachedPartial("PartialView", MyModel, 3600, true, contextualKeyBuilder: (model, viewData) => MyModel.UniqueKey)
    

    You'd have to have a property on your model that returns a unique value (eg. UniqueKey) for the model instance (or maybe override ToString() for this).

Please Sign in or register to post replies

Write your reply to:

Draft