DateTime.Now in Model C# ASP.NET MVC 4

Total Post:70


 6547  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.


    [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?

  1. Re: DateTime.Now in Model C# ASP.NET MVC 4

    Hi Samuel,

    Here's what you need:

    private DateTime? dateCreated;
    public DateTime DateCreated
        get { return dateCreated ?? DateTime.Now; }
        set { dateCreated = value; }

      Modified On Apr-04-2018 04:33:41 AM


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.