Razor中新的空条件运算符

Razor中新的空条件运算符

本文介绍了ASP.NET MVC Razor中新的空条件运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,自从C#6.0发布以来,我就一直在使用null条件运算符.示例:

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.

但是,当我在Razor中进行此操作时:

However, when I go this in Razor:

@Model?.Person?.Zip

我的Model?被视为动态的,但?破坏了动态的事物,其余的则呈现为文本.

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

您如何解决这个问题?

推荐答案

只是一个猜测

@(Model?.Person?.Zip)

这篇关于ASP.NET MVC Razor中新的空条件运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 09:40