如何在可空的datetime列上使用ToShortDateString()

Val*_*lip 0 c# asp.net datetime

ToShortDateString()当datetime列允许空值时,如何使用方法?

我在这个asp.net代码上收到以下错误<%= Model.EndDate.ToShortDateString() %>:

'System.Nullable'不包含'ToShortDateString'的定义,并且没有可以找到接受类型'System.Nullable'的第一个参数的扩展方法'ToShortDateString'(你是否缺少using指令或汇编引用?)

Rob*_*ito 5

这样做的正确方法就像在这个例子中:

<%=Model.EndDate.HasValue ? Model.EndDate.Value.ToShortDateString() : string.Empty; %>
Run Code Online (Sandbox Code Playgroud)

Nullable值总是暴露一个名为"Value"的属性,如果不为null则包含数据,另一个属性称为"HasValue",指示数据是否存在.