Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

So since C# 6.0 came out, I've been using the null-conditional operator quite a lot. Example:

Model?.Person?.Zip

However, I now have a situation where I have a solution where the customer operates on domain models in the view. While I would hunt down the developer with an axe, I find it easier to just do some null checks in the view.

However, when I go this in Razor:

@Model?.Person?.Zip

My Model? is seen as dynamic, but ? breaks the dynamic things and rest is rendered as text.

How do you solve this?

like image 388
Lars Holdgaard Avatar asked Oct 16 '15 13:10

Lars Holdgaard


People also ask

How to use null conditional operator in C#?

Available in C# 6 and later, a null-conditional operator applies a member access, ?. , or element access, ?[] , operation to its operand only if that operand evaluates to non-null; otherwise, it returns null . That is, If a evaluates to null , the result of a?. x or a?[x] is null .

What is Razor syntax in ASP NET MVC?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML.

What is null conditional and null coalescing in C#?

it is used to define a default value for nullable value types or reference types. It returns the left-hand operand if the operand is not null; otherwise, it returns the right operand. In cases where a statement could return null, the null-coalescing operator can be used to ensure a reasonable value gets returned.

What is razor in MVC What are the main Razor syntax rules?

Razor is a simple programming syntax for embedding server code in web pages. Razor syntax is based on the ASP.NET framework, the part of the Microsoft.NET Framework that's specifically designed for creating web applications.


4 Answers

Just a guess

@(Model?.Person?.Zip)
like image 191
Dieter B Avatar answered Oct 08 '22 00:10

Dieter B


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).

like image 36
Eilon Avatar answered Oct 07 '22 23:10

Eilon


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
like image 26
sunrunner20 Avatar answered Oct 07 '22 22:10

sunrunner20


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)
like image 41
Masoud Darvishian Avatar answered Oct 07 '22 23:10

Masoud Darvishian