Copied to clipboard

Flag this post as spam?

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


  • Rick van der Meij 35 posts 135 karma points
    Oct 03, 2014 @ 10:24
    Rick van der Meij
    0

    MVC macroPartial parameter vs ViewDataDictionary

    I'm curious about the differences between the use of parameters and viewdatadictionary.

    When should I render a partial with a viewdatadictionary and when should i render a macro partial with parameters. For example.

    @Html.Partial("ToTop", new ViewDataDictionary { { "url", "#article" }, { "text", "Top" } })
    <a href="@Umbraco.Coalesce(ViewBag.Url, "#body")" class="btn btn-totop">@Umbraco.Coalesce(ViewBag.Text, "Top")</a>
    

    or

    @Umbraco.RenderMacro("ToTop", new {  bookmark ="#article", toptext = "Top" })
    <a href="@Umbraco.Coalesce(Model.MacroParameters["bookmark"], "body")" class="btn btn-totop">@Umbraco.Coalesce(Model.MacroParameters["toptext"], "Top")</a>
    

    Are there differences in use, speed, caching etc?. As far as I can tell the only use of a macro partial is to use it within a RichTextEditor

  • Charles Roper 48 posts 200 karma points
    Mar 22, 2016 @ 22:40
    Charles Roper
    0

    I came to the forum with this exact same question. Made a sad face when I saw no replies.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 23, 2016 @ 08:07
    Dave Woestenborghs
    1

    Hi Rick, Charles,

    The version with the @Html.Partial doesn't get cached.

    The version with macro can be cached depending on your macro cache settings. The advantage is that when using macro parameters the macro can be reused. Also the macro get's cached depending on the parameter values. So when you use the same macro twice on your page with different cache settings than these will be cached seperatly.

    Umbraco also supports rendering of cached partial views using :

    @Html.CachedPartial("MyPartialName", new MyModel(), 3600)
    

    Full documentation can be read here in the caching part : https://our.umbraco.org/documentation/Reference/Templating/Mvc/partial-views

    Dave

  • Charles Roper 48 posts 200 karma points
    Mar 23, 2016 @ 08:59
    Charles Roper
    0

    Thanks Dave.

    So other than caching, which can be worked around by using @HtmlCachedPartial, there's no difference other than how they look?

    I think how they look and read is a factor here also (to my eyes anyway). The macro version looks easier to read, more expressive. Passing in the ViewDataDictionary looks kinds of hacky in comparison, especially if it only has one parameter.

    There's a good explanation of the ViewDataDictionary syntax on StackOverflow if it's confusing to anyone.

Please Sign in or register to post replies

Write your reply to:

Draft