Antaris/RazorEngine — Issue #21: Subclassed models don't carry through to the Layout
Author: korggy
Created: 2012-01-30 19:27:22+00:00
State: open

It's easiest to describe this bug via an example...

The following works:

public class Model {}

View.cshtml:
@model Model
@{ _Layout = "Layout"; }
...

Layout.cshtml:
@model Model
...

The following doesn't work:

public class Model {}
public class Model2 : Model {}

View.cshtml:
@model Model2
@{ _Layout = "Layout"; }
...

Layout.cshtml:
@model Model
...

The first example works fine but when I try to parse the 2nd example, I get the following exception:

System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
Source=l1lqrdfo
StackTrace:
at CompiledRazorTemplates.Dynamic.cddfefecbdb.Execute()
at RazorEngine.Templating.TemplateBase.RazorEngine.Templating.ITemplate.Run(ExecuteContext context)
at RazorEngine.Templating.TemplateBase.RazorEngine.Templating.ITemplate.Run(ExecuteContext context)
at RazorEngine.Templating.TemplateService.Run(ITemplate template)
at RazorEngine.Templating.TemplateService.Parse[T](String razorTemplate, T model, String name)
at ConsoleApplication2.Program.Main4() in d:\Temp\ConsoleApplication2\Program.cs:line 62
at ConsoleApplication2.Program.Main(String[] args) in d:\Temp\ConsoleApplication2\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: