Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 6 syntax in Razor View

Tags:

razor

c#-6.0

<input type="hidden" value="@ViewData["LoginProvider"]?.ToString() ?? null" />

When the ViewData is null, it makes a half-conversion and the hidden field contains value: ?.ToString() ?? null

I'm sure that I've enabled C# 6:

<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701">
    <providerOption name="CompilerVersion" value="v4.0" />
</compiler>

Why?

like image 535
Tân Avatar asked Jan 07 '16 02:01

Tân


People also ask

What is %d in C programming?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.

Why is C used?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


1 Answers

There is a problem with the ?? operator, the left hand value is string and the right hand value is null, therefore, it is not a correct syntax. Also, since there is an assignment operation involved, you need to wrap them in parenthesis.

You can try the following:

<input type="hidden" value="@(ViewData["LoginProvider"]?.ToString() ?? "")" />
like image 97
Felix Cen Avatar answered Sep 28 '22 16:09

Felix Cen