5

I have two cshtml-files in the same subfolder of Views. One of the templates is meant to include the other template. I tried to accomplish that as follows:

Main template:

<html>
  <head></head>
  <body>
    @Html.Partial("~/Views/Pdfs/Header");
  </body>
</html>

The error I get is

Unable to compile template. The name 'Html' does not exist in current context.

What am I supposed to do additionally?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
sk904861
  • 1,247
  • 1
  • 14
  • 31
  • possible duplicate of [RazorEngine issues with @Html](http://stackoverflow.com/questions/8561164/razorengine-issues-with-html) – Erik Philips Oct 24 '14 at 18:10

2 Answers2

3

As commented by Erik there is no Html in RazorEngine (see the linked answer), however you can use @Include("mytemplate") instead.

If you want to be compatible with the @Html.Partial() syntax for some reason you can extend the RazorEngine syntax like this.

Basically what you want to do is provide your own class inheriting from TemplateBase<T> (or ITemplate to be exact) and then set it either via configuration or the @Inherit MyBaseClass<MyModel> syntax. In this case you could just call the Include method from your Partial method within the Html helper class.

matthid
  • 1,644
  • 16
  • 26
1

Been annoyed by this for a long time. Wrote all the infrastructure classes to just get this working like you'd expect in MVC, without all the MVC burden:

var razor = RazorHelper.O;
var html = razor.RenderFromMvc(@"Views\RazorEngine\TestEmail.cshtml", vm);

https://github.com/b9chris/RazorEngineComplete

Chris Moschini
  • 36,764
  • 19
  • 160
  • 190