Copied to clipboard

Flag this post as spam?

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


  • Jeroen Oostwouder 104 posts 300 karma points
    Sep 11, 2015 @ 13:17
    Jeroen Oostwouder
    0

    Html.Raw() within a Helpers.cshtml

    I'm not that advanced with Umbraco, but I'm having some trouble with the following:

    I found it's a "best practice" to put all @Helper functions within a seperate file. So I have App_Code\Helpers.cshtml

    The problem is that I need Html.Raw(), but it gives me a null-reference on the Html object. Do I need a special inherit on the top of my page to be able to use this object? I have none at the moment.

  • Marc Goodson 2157 posts 14411 karma points MVP 9x c-trib
    Sep 11, 2015 @ 22:53
    Marc Goodson
    0

    When you move your razor helpers into the App_Code, in MVC, I think you lose access to the Html helpers via the @Html. syntax.

    workaround wise you can pass the HtmlHelper class into your helper method eg

    @helper MyHelperMethod(HtmlHelper html, String cssClass) {
        var extra = "class=\"foo\"";
        <[email protected](extra)></div>
    }
    

    or instead, create your helper methods in a c# class as extension methods of the HtmlHelper class eg

    public static class HtmlHelperExtensions
    {
        public static MvcHtmlString MyHelperMethod(this HtmlHelper)
        {
            var div = new TagBuilder("div");
            div.AddCssClass("foo");
            return MvcHtmlString.Create(div.ToString());
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft