Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6190703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:38:11+00:00 2026-05-24T02:38:11+00:00

I have the following class that’s used by my MVC3 application. I would like

  • 0

I have the following class that’s used by my MVC3 application. I would like
to simplify the updating of the class so that when a new class object is
created then the Created and CreatedBy fields get set automatically.

I’d also like to make it so that the Modified and ModifiedBy fields get
updated automatically.

Is there a way that I could do this?

The class is used in MVCnamespace Storage.Models
{
    public class Topic : TableServiceEntity
    {
        [DisplayName("Partition Key")]
        public override string PartitionKey { get; set; }
        [DisplayName("Row Key")]
        public override string RowKey { get; set; }
        [DisplayName("Description")]
        public string Description { get; set; }
        public DateTime Created { get; set; }
        public DateTime Modified { get; set; }
        public String CreatedBy { get; set; }
        public string ModifiedBy { get; set; }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:38:11+00:00Added an answer on May 24, 2026 at 2:38 am

    jonathan,

    Jason’s answer above with the logic contained within the constructors is a perfectly valid and clean way of doing this and I wouldn’t argue with that (and have done it myself for more ‘static’ properties). However, given that there could be a timelapse between the creation of the object and the actual save, then you may also want to consider putting this logic into your controller (or service layer).

    this would look roughly like this:

    public ActionResult Create(MyCreateViewModel viewModel)
    {
        if (ModelState.IsValid)
        {
            viewModel.Entity.Created = DateTime.UtcNow;
            _myService.Insert(viewModel.Entity);
            _myService.SaveChanges();
            return this.RedirectToAction(x => x.Index());
        } else {
            PopulateViewModel(viewModel);
            return View(viewModel);
        }
    }
    

    likewise, you may have a LastEdit datetime that you want to track. use the Edit action similarly:

    public ActionResult Edit(MyEditViewModel viewModel)
    {
        if (ModelState.IsValid)
        {
            viewModel.Entity.LastEditDate= DateTime.UtcNow;
            _myService.AttachAndUpdate(viewModel.Entity);
            _myService.SaveChanges();
            return this.RedirectToAction(x => x.Index());
        } else {
            PopulateViewModel(viewModel);
            return View(viewModel);
        }
    }
    

    just another approach to ensure that datetime related properties are truly reflected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class that I would like to do a deep copy
I have a class that looks like the following: public class MyClass { private
We have a class a class that looks something like the following: public class
I have a class that implements many internal interfaces and I would like that
I have the following class that I'd like access to within certain controllers in
I have created the following class that extends JSpinner to iterate over dd/mm/yyy values.
I have a class that looks like the following: class myTexture { public: myTexture();
I have the following class that is used to hold data for reporting only.
I have a client class that looks like the following: class MyClient{ Service service;
I have the following class written by someone else that I'm trying to understand

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.