asp.net-mvc - 当日期列等于特定值时如何以不同方式显示?
问题描述
在一个视图中,我正在显示日期列 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'),
解决方案
您为什么不按照这些思路做一些事情,根据您的 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" } })
推荐阅读
- vue.js - Vue,访问组件方法
- go - Golang:reflect.DeepEqual 返回意外的错误
- java - 我可以通过更改方法的返回类型来实现方法重载吗
- c++ - 让我的 BST 在递归函数中连接有些麻烦
- javascript - 获取突出显示的文本而不是突出显示的文本值
- javascript - 从浏览器获取当前 URL 并应用正则表达式
- c++ - 浮点数据类型误解
- pip - python3 与 pip3 有不同的目录
- python - 如何在python 3中从unicode打印正确的字符,如“\\u201c借\\u201d东风”?
- slack-api - 如何获取发送 Slack 临时消息的令牌