Antaris/RazorEngine

Does not exist in the current context

matthew798 opened this issue · 2 comments

I have a view in DB:

@using Microsoft.AspNetCore.Mvc.Localization
@inject  IViewLocalizer Localizer
@model  dynamic

@section Scripts
{
    @if(Model != null)
    {
        <script>
            var json = @Html.Raw(Json.Serialize(@Model));
            populateForm($("#form"), json);
        </script>
    }
}

@{
    ViewData["Title"] = Localizer["Preventive Maintenance Postponement"];
}

<h1>@Localizer["Preventive Maintenance Postponement"]</h1>

<form asp-controller="Form" asp-action="Submit" method="post" id="form">
...
</form>

I tried to compile and run the view like so:
var result = Engine.Razor.RunCompile(form.Html, form.Name, typeof(Form), form);

And I get:

An exception of type 'RazorEngine.Templating.TemplateCompilationException' occurred in RazorEngine.NetCore.dll but was not handled in user code
Errors while compiling a Template.
Please try the following to solve the situation:
  * If the problem is about missing/invalid references or multiple defines either try to load 
    the missing references manually (in the compiling appdomain!) or
    Specify your references manually by providing your own IReferenceResolver implementation.
    See https://antaris.github.io/RazorEngine/ReferenceResolver.html for details.
    Currently all references have to be available as files!
  * If you get 'class' does not contain a definition for 'member': 
        try another modelType (for example 'null' to make the model dynamic).
        NOTE: You CANNOT use typeof(dynamic) to make the model dynamic!
    Or try to use static instead of anonymous/dynamic types.
More details about the error:
 - error: (15, 18) The name 'inject' does not exist in the current context
	 - error: (17, 18) The name 'model' does not exist in the current context
	 - error: (19, 18) The name 'section' does not exist in the current context
	 - error: (24, 36) The name 'Html' does not exist in the current context
	 - error: (24, 45) The name 'Json' does not exist in the current context
	 - error: (29, 4) The name 'ViewData' does not exist in the current context
	 - error: (29, 24) The name 'Localizer' does not exist in the current context
	 - error: (31, 18) The name 'Localizer' does not exist in the current context
	 - error: (33, 20) The name 'Localizer' does not exist in the current context
	 - error: (35, 46) The name 'Localizer' does not exist in the current context
	 - error: (37, 51) The name 'Localizer' does not exist in the current context
	 - error: (39, 45) The name 'Localizer' does not exist in the current context
	 - error: (41, 54) The name 'Localizer' does not exist in the current context
	 - error: (43, 47) The name 'Localizer' does not exist in the current context
	 - error: (45, 45) The name 'Localizer' does not exist in the current context
	 - error: (47, 54) The name 'Localizer' does not exist in the current context
	 - error: (49, 52) The name 'Localizer' does not exist in the current context
	 - error: (57, 18) The name 'Localizer' does not exist in the current context
	 - error: (59, 48) The name 'Localizer' does not exist in the current context

I realize I might have to work on getting the localizer to work. But AFAIK the @model should work just fine. Any ideas?

texyh commented

@matthew798 did you manage to fix this?

@texyh no sorry, I gave up and did it differently.