Home > DeveloperSection > Forums > DateTime.Now in Model C# ASP.NET MVC 4
Simons Hood
Simons Hood

Total Post:70

Points:494
Posted on    March-06-2014 11:08 PM

 ASP.NET MVC ASP.NET MVC 
Ratings:


 1 Reply(s)
 3639  View(s)
Rate this:

I have created a model for Articles, and in the articles I am trying to implement a date created on initial create.

//Model

    [Display(Name = "Date Created")]

    public DateTime dateCreated

    {

        get { return DateTime.Now; }

    }

It works well when I create an article, but it also sets the same date time value to all the other articles. :/

Is there a way around this?



Pravesh Singh

Total Post:411

Points:2881
Posted on    March-06-2014 11:19 PM

Hi Samuel,

Here's what you need:

private DateTime? dateCreated;

public DateTime DateCreated

{

    get { return dateCreated ?? DateTime.Now; }

    set { dateCreated = value; }

}


Don't want to miss updates? Please click the below button!

Follow MindStick