I am building a very basic MVC3 site while I learn and I am having difficulty with the following declarative Razor html helper.
Inside RMB.cshtml inside App_Code folder:
@helper ReplaceCrLf(string strText)
{
@Html.Raw(Html.Encode(strText).Replace(Environment.NewLine, "<br />"));
}
Inside my index.cshtml view:
@RMB.ReplaceCrLf(Model.Post)
This gives me a null reference exception on Html in the helper, because it doesn't seem to know what it is. I can work around this by passing Html from the view to the helper, but I was wondering if there is another way for my shared html helpers to be able to reference Html without me having to pass it in to ever helper I write?
For completeness, here is the working workaround:
In RMB.cshtml in App_Code
@helper ReplaceCrLf(string strText, System.Web.Mvc.HtmlHelper Html)
{
@Html.Raw(Html.Encode(strText).Replace(Environment.NewLine, "<br />"));
}
In index.cshtml view
@RMB.ReplaceCrLf(Model.Post, Html)