How do I get value from view model in Javascript if the property is nullable like int?, bool? etc.
I easily managed to check if property has a value, it works until I return it's value.
<script type="text/javascript">
// Works as expected
var year = !!'@Model.Year' ? 1994 : 0;
// Null reference exceptin thrown
var year2 = !!'@Model.Year' ? @Model.Year.Value : 0;
</script>
For some reason the condition is ignored when returning property using razor syntax. The same happens if use if and else statements.
Edit:
The exception is thrown also if using @Model.Year.HasValue.ToString().ToLower() to check for null value.