Nullable Datetime Render - .Net Core 2.0
Hi,
I had some unit test working great at RazorLight version 1.0.0-rc2 and .net core 1.0.0. However, I am porting all those stuffs to .net core 2.0 (netstandard2.0), and some exceptions has been throws.
Considerations:
- My .csproj already contains
PreserveCompilationContext True. - RazorLight version 2.0.0-alpha3
Model Class:
public class MyModel
{
public DateTime? Creation { get; set; }
}
Template to be Render:
@(Model.Creation.Value.Date) - @(Model.Creation.Value.Date)
RazorLight 1.0.0-rc2 code which was working:
var engine = EngineFactory.CreatePhysical(Path.GetTempPath());
var result = engine.ParseString(_template, data, data.GetType());
RazorLight 2.0.0-alpha3 porting:
var key = "key";
var engine = new EngineFactory().ForFileSystem(Path.GetTempPath());
var result = engine.CompileRenderAsync(key, _template, data, data.GetType())
.GetAwaiter()
.GetResult();
Exception throwed:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: ''System.DateTime' does not contain a definition for 'Value''
Maybe I am missing something in migration, but it seems to be ignoring Nullable Type from Creation property.
Thank you
May I ask you to zip your project as a piece of sample and send it to me?
No need, I reproduced it myself. Very strange bug
I guess I found the problem. It's not a bug. In RazorLight 2.0 you should specify Model Type directly in a template
@model MyProject.MyModel
Otherwise your template model will be dynamic. And dynamic does not support nullable types
Yeah, you are right, it has been solved! Thank you very much.
For suggestion, inside CompileRenderAsync in overload that receives Model Type parameter or througth Generics, the codes know that it is a "Strong Type" and not Dynamic. If Template does not contains explicit Model Type declared, maybe the Lib could add it.
PS: However, It is not clear for me if the Lib should be responsible to change Template string.
It used to be in 1.0 version, but I wanted to make it as similar as possible to ASP.NET MVC, so that's how it works now
This is still a problem for a legitimate use of dynamic properties, even with explicit model declaration at the top of the template. Example:
Model:
public class TemplateContext
{
public dynamic Person { get; set; }
}
public class Person
{
public DateTime? Age { get; set; }
}
Template:
@model TemplateContext
if (Model.Person.Age.HasValue)
{
<div>Age is @Model.Person.Age.Value</div>
}
This line in the template if (Model.Person.Age.HasValue) will throw this exception:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
HResult=0x80131500
Message='System.DateTime' does not contain a definition for 'HasValue'
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
It's also the case with int? so I think this is a problem with all nullable types. For some reason RazorLight thinks that nullable types are the non-nullable equivalent instead.
The workaround is to do this instead:
@model TemplateContext
if (Model.Person.Age != null)
{
<div>Age is @Model.Person.Age</div>
}
...but HasValue and Value should still work.
dynamic does not support nullable types