我想创建一个扩展方法,它可以测试 null 的强类型属性值,如果为 null,则取一个替换值,即“N/A”,否则运行原始代码行。显式版本是:
@(Model.Birthdate == null ? "N/A" : Model.Birthdate.Value.Date.ToString("dd/MM/yyyy"))
而不是将此测试应用于视图中的所有值,我想知道是否有使用扩展方法的更雄辩的方法,有点像:
@(Model.Birthdate.Value.TestForNull("N/A"))..... I am trying to write my idea out and am not sure it is possible with the date formatting as well! Perhaps one has to do it line by line.