Randy Minder
Randy Minder

Reputation: 48522

RazorEngine - How do I use my own HTML helper?

I have the following in a RazorEngine template:

@using WAND.Helpers
@model Tour.Entities.Accommodation

<p>
    You can view your WAND accommodation home page by @Html.HomepageActionLink("Accommodation","clicking here",@Model.AccommodationId)
</p>

The HomePageActionLink is a helper I wrote that looks, in part, like this:

    public static MvcHtmlString HomepageActionLink(this HtmlHelper helper, string serviceProviderType, string linkText, int idValue )
    {
        ...

        return new MvcHtmlString(link);
    }

The problem is that when I run this, I get an error saying:

More details about the error: - error: (64, 60) The name 'Html' does not exist in the current context 

Is there a way I can get this to work?

Upvotes: 0

Views: 566

Answers (1)

Andrus
Andrus

Reputation: 27955

You can override BaseTemplateType with class which has Html implementation. Like

var config = new TemplateServiceConfiguration();
        // MvcHtmlStringFactory code from 
        // http://stackoverflow.com/questions/19431365/razorengine-html-helpers-work-but-escape-the-html
        config.EncodedStringFactory = new MvcHtmlStringFactory();

        // HtmlTemplateBase code from
        // http://stackoverflow.com/questions/8561164/razorengine-issues-with-html
        config.BaseTemplateType = typeof(HtmlTemplateBase<>);
        var service = RazorEngineService.Create(config);

        var Colmodel ="@Html.BeginForm(\"UploadDocument\", \"Detail\", new{a=1,b=2})";

        string res = service.RunCompile(Colmodel, Colmodel,
            model.GetType(), model);

Upvotes: 1

Related Questions