Razor exception compiling template

2.8k views Asked by At

I'm trying to use Postal to send out emails from a service (not in an ASP.NET project). I keep getting exceptions with the following message:

error CS0103: The name 'model' does not exist in the current context

I'm following the tutorial from the Postal wiki: https://github.com/andrewdavey/postal/wiki/Postal-in-non-web-scenario

My template looks like:

@model Namespace1.AlertEmailViewModel

From: [email protected]
To: @Model.FirstName @Model.LastName <@Model.Email>
Subject: Alert! @Model.ShortDescription

(The model class in question does exist.)

Any help would be appreciated. Thanks!

2

There are 2 answers

0
Matthew Abbott On BEST ANSWER

As you are using Postal in a non-web scenario, under the hood it is using our RazorEngine project. Currently, this means it is utilising the v2.1 release currently pushed on NuGet.

The @model syntax isn't supported natively in v2.1, but the upcoming v3 release includes this support, as well as other nice things like layouts, thread-safety etc. (https://github.com/Antaris/RazorEngine)

2
Morten Mertner On

I've had this issue in the past for projects that were created using an earlier version of MVC.

To resolve it, if I remember correctly, add this to your App.config file:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>