首页 > 解决方案 > 当日期列等于特定值时如何以不同方式显示?

问题描述

在一个视图中,我正在显示日期列 bcp,目前它默认为“1900-01-01”。当 bcp='1900-01-01' 在 Editorfor 中显示 null 时,如何在视图中显示?

模型:

 public partial class Training_test
    {
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
        public System.DateTime bcp { get; set; }

}

看法:

@Html.EditorFor(model => model.bcp, new { htmlAttributes = new { @class = "  datepicker" } })

SQL 脚本:

CREATE TABLE Training_test(
    [cart_id] [int] IDENTITY(1,1) NOT NULL,

    [bcp] [date] NOT NULL CONSTRAINT [DF_TrainingChecklist_bcp_1_test]  DEFAULT ('1900-01-01'),

标签: asp.net-mvc

解决方案


您为什么不按照这些思路做一些事情,根据您的 bcp 值创建一个计算变量?这应该是这样的:

   //in your class
  public DateTime BcpStr => bcp.equals(new DateTime(1900,1,1) ? null : bcp; 

   //in your view
  @Html.EditorFor(model => model.BcpStr , new { htmlAttributes = new { @class = "  datepicker" } })

推荐阅读