The new null-conditional operator in ASP.NET MVC Razor

Just change the target framework to .NetFramework 4.7 and install these packages using Nuget package manager:

  • Microsoft.CodeDom.Providers.DotNetCompilerPlatform
  • Microsoft.Net.Compilers

Then use it like this (note the parenthesis which allow full C# syntax as opposed to partial Razor syntax):

@(Model.Country?.Name)

This can also happen when you're missing one or both of the following NuGet packages from the project:

  • Microsoft.CodeDom.Providers.DotNetCompilerPlatform
  • Microsoft.Net.Compilers

Just a guess

@(Model?.Person?.Zip)

For some additional completeness (I work on the ASP.NET team at Microsoft):

As Dieter B (and some others) correctly note, @(Model?.Person?.Zip) will work.

The @(...) syntax can be thought of as an "escape syntax" that allows far more flexibility in terms of which code will be parsed as the expression.

When the current version of Razor was built, only C# 5 was around, so the new C# 6 syntaxes were not directly supported.

The ASP.NET team is looking to back-port some of the Razor v4 (used in ASP.NET 5 / MVC 6) support for C# 6 back to Razor v3 (used in ASP.NET 4.x / MVC 5).