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.
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());
}
}
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.
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
or instead, create your helper methods in a c# class as extension methods of the HtmlHelper class eg
is working on a reply...