Asp.net 插入日期时间
我有一个像这样的帖子基类Asp.net 插入日期时间,asp.net,asp.net-mvc-4,datetime,annotations,Asp.net,Asp.net Mvc 4,Datetime,Annotations,我有一个像这样的帖子基类 public partial class Post { public int Id { get; set; } public string Title { get; set; } public DateTime PostDate { get; set; } public virtual ICollection<Tag> Tag { get; set; } } 公共部分类职位 { 公共int Id{get;set;} 公共字
public partial class Post
{
public int Id { get; set; }
public string Title { get; set; }
public DateTime PostDate { get; set; }
public virtual ICollection<Tag> Tag { get; set; }
}
公共部分类职位
{
公共int Id{get;set;}
公共字符串标题{get;set;}
public DateTime PostDate{get;set;}
公共虚拟ICollection标记{get;set;}
}
我想让PostDate
在创建记录时总是有一个值,而不是在表单中指定它。也许你们知道一些好的解决方案,也许有一些注释或其他方法可以做到这一点?只需使用以下方法:
post.PostDate = DateTime.UtcNow;
创建新帖子时添加它。或者,当您使用
default
在类的构造函数中设置它时,可以在sql中创建它。创建新实例时将调用它
public partial class Post
{
public Post()
{
PostDate = DateTime.Now; // or DateTime.UtcNow;
}
public int Id { get; set; }
public string Title { get; set; }
public DateTime PostDate { get; set; }
public virtual ICollection<Tag> Tag { get; set; }
}
公共部分类职位
{
公职人员职位()
{
PostDate=DateTime.Now;//或DateTime.UtcNow;
}
公共int Id{get;set;}
公共字符串标题{get;set;}
public DateTime PostDate{get;set;}
公共虚拟ICollection标记{get;set;}
}
我想下面会对你有所帮助
[Required]
[DataType(DataType.Date, ErrorMessage="Your message")]
public DateTime? PostDate { get; set; }