在 ASP.Net MVC 中设置修改和创建日期时间
Set Modify and Create DateTime in ASP.Net MVC
我正在尝试创建我自己的 CMS 博客用于教育目的,几乎每个博客在每篇文章中都有一个参考,通常是这样说的:
Updated: April 1, 1999 | Published: April 1, 1999 | Created: April 1, 1999
我将我的模型设置为允许通过视图表单手动输入,但我假设在创建表单时可以通过模型执行此操作。
我知道我应该使用 DateTime.UTCNow
来建立它,但我不确定我应该如何触发它。
型号
using System;
using System.Data.Entity;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Web.Mvc;
namespace JosephMCasey.Areas.Article.Models
{
public class Articles
{
[Key]
public int PostId { get; set; }
[Required]
[StringLength(256, MinimumLength = 1, ErrorMessage = "Title cannot be longer than 256 characters.")]
public string Title { get; set;}
[Display(Name = "Create Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? CreateDate { get; set; }
[Display(Name = "Publish Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? PublishDate { get; set; }
[Display(Name = "Modify Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? ModifyDate { get; set; }
public string Author { get; set; }
[Required]
[NotMapped]
public MvcHtmlString PageBody { get; set; }
public string Body
{
get { return PageBody.ToHtmlString(); }
set { PageBody = new MvcHtmlString(value); }
}
// Feature is the featured image of each blog
public string Feature { get; set; }
public bool Published { get; set; }
public string Excerpt { get; set;}
[Display(Name = "GMT of Create Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? CreateDateUTC { get; set; }
[Display(Name = "UTC of Publish Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? PublishDateUTC { get; set; }
[Display(Name = "GMT of Modify Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? ModifyDateUTC { get; set; }
public string CanonicalUrl { get; set; }
public string UrlSlug { get; set; }
public string Category { get; set; }
public string Tag { get; set; }
}
public class ArticleDBContext : DbContext
{
public DbSet<Articles> Articles { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Tag> Tags { get; set; }
}
}
可能的扩展方法?
//Where should the extension method be coded?
internal static void SyncObjectsStatePreCommit(this DbContext dbContext)
{
foreach (var dbEntityEntry in dbContext.ChangeTracker.Entries())
{
var trackableObject = dbEntityEntry.Entity as ITrackable;
// we need to set/update trackable properties
if (trackableObject == null)
{
continue;
}
var dateTime = DateTime.Now;
var dateTimeUTC = DateTime.UtcNow;
// set createddate only for added entities
if (entityState.ObjectState == ObjectState.Added)
{
trackableObject.CreateDate = dateTime;
trackableObject.CreateDateUTC = dateTimeUTC;
}
// set LastUpdatedDate for any case other than Unchanged
if (entityState.ObjectState != ObjectState.Unchanged)
{
trackableObject.ModifyDate = dateTime;
trackableObject.ModifyDateUTC = dateTimeUTC;
}
}
}
找到类似问题
Handling Created and Modified Date in MVC
DateCreated and DateModified in ASP.NET MVC 5
我会在您将博客条目保存到数据库时设置所有这些。要么在 table 上设置触发器,要么在执行这些操作时在 C# 代码中设置它们。
您还应该将 Published
和 Modified
设置为 DateTime?
,因为您在最初创建博客条目时没有它们的数据。有人可能会争辩说创建和发布可以是同一个任务,所以它们可以耦合,但我仍然认为您可以在发布博客之前先创建一个博客。
我正在尝试创建我自己的 CMS 博客用于教育目的,几乎每个博客在每篇文章中都有一个参考,通常是这样说的:
Updated: April 1, 1999 | Published: April 1, 1999 | Created: April 1, 1999
我将我的模型设置为允许通过视图表单手动输入,但我假设在创建表单时可以通过模型执行此操作。
我知道我应该使用 DateTime.UTCNow
来建立它,但我不确定我应该如何触发它。
型号
using System;
using System.Data.Entity;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Web.Mvc;
namespace JosephMCasey.Areas.Article.Models
{
public class Articles
{
[Key]
public int PostId { get; set; }
[Required]
[StringLength(256, MinimumLength = 1, ErrorMessage = "Title cannot be longer than 256 characters.")]
public string Title { get; set;}
[Display(Name = "Create Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? CreateDate { get; set; }
[Display(Name = "Publish Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? PublishDate { get; set; }
[Display(Name = "Modify Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? ModifyDate { get; set; }
public string Author { get; set; }
[Required]
[NotMapped]
public MvcHtmlString PageBody { get; set; }
public string Body
{
get { return PageBody.ToHtmlString(); }
set { PageBody = new MvcHtmlString(value); }
}
// Feature is the featured image of each blog
public string Feature { get; set; }
public bool Published { get; set; }
public string Excerpt { get; set;}
[Display(Name = "GMT of Create Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? CreateDateUTC { get; set; }
[Display(Name = "UTC of Publish Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? PublishDateUTC { get; set; }
[Display(Name = "GMT of Modify Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? ModifyDateUTC { get; set; }
public string CanonicalUrl { get; set; }
public string UrlSlug { get; set; }
public string Category { get; set; }
public string Tag { get; set; }
}
public class ArticleDBContext : DbContext
{
public DbSet<Articles> Articles { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Tag> Tags { get; set; }
}
}
可能的扩展方法?
//Where should the extension method be coded?
internal static void SyncObjectsStatePreCommit(this DbContext dbContext)
{
foreach (var dbEntityEntry in dbContext.ChangeTracker.Entries())
{
var trackableObject = dbEntityEntry.Entity as ITrackable;
// we need to set/update trackable properties
if (trackableObject == null)
{
continue;
}
var dateTime = DateTime.Now;
var dateTimeUTC = DateTime.UtcNow;
// set createddate only for added entities
if (entityState.ObjectState == ObjectState.Added)
{
trackableObject.CreateDate = dateTime;
trackableObject.CreateDateUTC = dateTimeUTC;
}
// set LastUpdatedDate for any case other than Unchanged
if (entityState.ObjectState != ObjectState.Unchanged)
{
trackableObject.ModifyDate = dateTime;
trackableObject.ModifyDateUTC = dateTimeUTC;
}
}
}
找到类似问题
Handling Created and Modified Date in MVC
DateCreated and DateModified in ASP.NET MVC 5
我会在您将博客条目保存到数据库时设置所有这些。要么在 table 上设置触发器,要么在执行这些操作时在 C# 代码中设置它们。
您还应该将 Published
和 Modified
设置为 DateTime?
,因为您在最初创建博客条目时没有它们的数据。有人可能会争辩说创建和发布可以是同一个任务,所以它们可以耦合,但我仍然认为您可以在发布博客之前先创建一个博客。