I am trying to create my own CMS Blog for education purposes, and nearly every Blog has a reference in each article that generally says something like:
Updated: April 1, 1999 | Published: April 1, 1999 | Created: April 1, 1999
I have my model set up to allow manual entry of this through the view form, but I am assuming there is a way to do this via the model when the form is created.
I know I should be using DateTime.UTCNow
to establish it, but I'm not sure how I should be triggering it.
Model
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; }
}
}
Possible Extension Method?
//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;
}
}
}