c# - 如何检查 cshtml 中的 null/empty 值

标签 c# visual-studio-2010 asp.net-mvc-3 razor

<b>Start Date: </b>@employee["StartDate"].<br />

使用 MVC Razor 3/C#,如何检查 cshtml 中的 employee["StartDate"] 值是否为 null/空?所以如果是的话,我会显示:

<b>Start Date: </b>Unknown.<br />

我试过:

@if(employee["StartDate"] == null){<b>Start Date: </b>Unknown.<br />} 

但这不起作用。

最佳答案

尝试

<b>Start Date: </b>@(employee["StartDate"] ?? "Unknown").<br />

?? 返回左侧值,如果左侧值为 null,则返回右侧值。

关于c# - 如何检查 cshtml 中的 null/empty 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17282554/

相关文章:

c# - 在 Main() 中保持模块化?

C#/C++ : Launch an application and handle its I/O calls to the system

c++ - 如何在 Visual Studio 2010 中使用 SWIG 创建 DLL

asp.net-mvc-3 - "An object with the same key already exists in the ObjectStateManager..."将实体状态设置为已修改时抛出异常

c# - SelectMethod 为 ObjectDataSource 调用了两次

c# - 覆盖先前设置的 Typemock 隔离器的功能行为 (WillThrow()/DoInstead())

c# - Visual Studio 错误?

asp.net-mvc-3 - 在MVC3非强制性远程验证上强制重新验证

model-view-controller - ColdFusion Model Glue 与 ASP.NET MVC 3 的 @section 的等效项是什么?

c# - 如何调用接受来自 C# 的 stringstream 类型参数的 C++ DLL 函数?