Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse a Razor template with partials from a custom folder?

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?

like image 550
sk904861 Avatar asked Sep 29 '22 13:09

sk904861


2 Answers

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.

like image 196
matthid Avatar answered Oct 03 '22 23:10

matthid


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

like image 41
Chris Moschini Avatar answered Oct 03 '22 21:10

Chris Moschini