在ASP中新的空条件运算符。净MVC剃须刀

时间:2022-05-04 11:50:48

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

因此,自从c# 6.0出来以来,我一直在大量地使用空条件运算符。

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.

我的模型吗?是动态的,但是?中断动态内容,rest被呈现为文本。

How do you solve this?

你怎么解决这个问题?

3 个解决方案

#1


57  

Just a guess

只是一个猜测

@(Model?.Person?.Zip)

#2


17  

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

对于一些额外的完整性(我在ASP中工作)。NET团队在微软):

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

正如Dieter B(和其他一些)正确地指出的那样,@(模型?. person ?. zip)将有效。

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.

当构建了当前版本的Razor时,只有c# 5存在,所以新的c# 6语法没有直接支持。

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

ASP。NET团队正在寻找一些Razor v4(在ASP中使用)的反向移植。NET 5 / MVC 6)支持c# 6回到Razor v3(在ASP中使用)。净4。x / MVC 5)。

#3


0  

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

当你从项目中丢失了以下一个或两个NuGet包时,也会发生这种情况:

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

#1


57  

Just a guess

只是一个猜测

@(Model?.Person?.Zip)

#2


17  

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

对于一些额外的完整性(我在ASP中工作)。NET团队在微软):

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

正如Dieter B(和其他一些)正确地指出的那样,@(模型?. person ?. zip)将有效。

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.

当构建了当前版本的Razor时,只有c# 5存在,所以新的c# 6语法没有直接支持。

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

ASP。NET团队正在寻找一些Razor v4(在ASP中使用)的反向移植。NET 5 / MVC 6)支持c# 6回到Razor v3(在ASP中使用)。净4。x / MVC 5)。

#3


0  

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

当你从项目中丢失了以下一个或两个NuGet包时,也会发生这种情况:

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