0

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;
                    }
                }
            }

Similar Questions Found

Handling Created and Modified Date in MVC

DateCreated and DateModified in ASP.NET MVC 5

Community
  • 1
  • 1
Joseph Casey
  • 1,283
  • 1
  • 14
  • 34
  • My personal preference is to manage this in the SaveChanges and use an Interface for the declaration of DateTime properties I would use for any entity I would need to audit. See this [link](http://stackoverflow.com/a/20532092/1453651) for the approach I prefer. – Judge Bread May 06 '15 at 21:01

1 Answers1

3

I would set all of those when you save your blog entries to your database. Either have a trigger on the table, or set them in C# code as you're making each of those actions.

You should also set up Published and Modified as DateTime? as you don't have data for them when you initially create the blog entry. One could argue that creation and publishing can be the same task, so those could be coupled, but I would still leave the possibility that you create a blog before publishing it.

krillgar
  • 12,596
  • 6
  • 50
  • 86
  • [Updated] I definitely agree with the need to include the nullable ? and the Publish (I actually had this commented out because I was tossed up on the value of including it). So you're saying I can't do this via the model. That I need to include it in each View? – Joseph Casey May 06 '15 at 18:52
  • I'm not sure where you're talking about "Model" and "View". Your class `Articles` gets saved to a database of some sort, correct? When you call Save on an `Articles` (this should be singular), set the appropriate DateTime property before sending it to the database. – krillgar May 06 '15 at 18:55