Copied to clipboard

Flag this post as spam?

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


  • almanZT 9 posts 90 karma points
    Sep 25, 2019 @ 08:56
    almanZT
    0

    Accessing IUmbracoContextFactory in static extension class

    Hi, How can I access an IUmbracoContextFactory such as umbracohelper in an static class.

    So basically, I want to render a grid and I have an HtmlHelperExtention class with an GetGridHtml method and I want to query the content by its unique identifier.

    public static MvcHtmlString GetGridHtml(this HtmlHelper html, IUmbracoContextFactory Umbraco, int id, string propertyAlias = "contentGrid", string framework = "ContentGrid")
            {
                using (var cref = Umbraco.EnsureUmbracoContext())
                {
                    var cache = cref.UmbracoContext.Content;
                    var property = cache.GetById(id);
                    return html.GetGridHtml(property, propertyAlias, framework);
                }            
            }
    

    But i am getting an error when I want to use this extention method from my razor view

    @Html.GetGridHtml(this.UmbracoContext, Model.Id)
    

    Whats the best practice of passing in the umbraco context object to extention static class?

  • almanZT 9 posts 90 karma points
    Sep 25, 2019 @ 11:31
    almanZT
    0

    I solved this by passing the Umbraco helper object from my view as I read in the documentation

    All Razor views that Umbraco creates expose an UmbracoContext property which is the UmbracoContext, they expose an ApplicationContext property which is Umbraco's ApplicationContext.

     @Html.GetGridHtml(Umbraco, Model.Id)
    

    I hope this is a correct and best practice way of doing this

Please Sign in or register to post replies

Write your reply to:

Draft