-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nullables treated as non-nullable at runtime #33
Comments
I think I may have a lead. Trying to use Linq select was causing a
This can be caused by using Linq on dynamic models. The non-generic template base uses dynamic as the type for the whole page model. https://github.com/adoconnection/RazorEngineCore/blob/master/RazorEngineCore/IRazorEngineTemplate.cs I'm guessing there is some issue with dynamic typing going on here |
As I can see your view inherits different model Have a look on this test: public class TestModel
{
public DateTime? DateTime { get; set; }
}
public class TestTemplate2 : RazorEngineTemplateBase<TestModel>
{
public void Initialize(TestModel model)
{
this.Model = model;
}
} RazorEngine razorEngine = new RazorEngine();
DateTime? dateTime = DateTime.Now;
IRazorEngineCompiledTemplate<TestTemplate2> template = razorEngine.Compile<TestTemplate2>("DateTime: @Model.DateTime.Value.ToString()");
string actual = template.Run(instance => instance.Model = new TestModel()
{
DateTime = dateTime
});
Assert.AreEqual("DateTime: " + dateTime, actual); |
Also, I get same error as this if I do caching, see #28 |
Woops, I copied the wrong model statement. CampaignDesignerRequest is actually a property on the other model, but I had been running experiments on a simplified version to make sure the parent object wasn't part of the issue. I am caching as in issue #28. I'll have to test if the issue also happens with model-specific template caches. |
Ok. I ran a few tests. Using definitely typed templates as you did above, with model-specific caches, and by casting the cache solves the issue. It also works to cast types within the template, though that could be a bit dangerous. Definitely a good gotcha to have documented |
I'd be happy to contribute documentation if we want to promote it from an issue to the wiki or readme |
you are welcome to push updates in description and wiki :) |
I'll try to push some changes this week |
My model includes a
DateTime?
propertyMy view inherits the model
At write time, the intellisense recognizes CampaignStartDate as a nullable and shows an error hint if I try to run date string formatting without specifying
.Value
However when I run the code, it gives me the error
I've tested the same on other value types, like int and double, and get the same kind of error.
Versions
Any thoughts on potential causes?
The text was updated successfully, but these errors were encountered: