-1

I have been following the microsoft tutorial code first razor pages to create an sql database project. I want to display the created date and the updated date on my 'people' index page.

My model:

public class People
{
    public int ID { get; set; }
    public DateTime CreatedAt { get; set; }
    public DateTime UpdatedAt { get; set; }
    public Ctype Ctype { get; set; }
    [Required]
    [StringLength(50, ErrorMessage = "First name cannot be longer than 50 characters.")]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }
    [Required]
    [StringLength(50)]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }
}

My context

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
        modelBuilder.Entity<Demographic>().ToTable("Demogrphic");
        modelBuilder.Entity<Address>().ToTable("Address");
        modelBuilder.Entity<People>().ToTable("People")
            .Property(e => e.CreatedAt)
            .HasDefaultValueSql("GETDATE()");
        modelBuilder.Entity<People>().ToTable("People")
            .Property(e => e.UpdatedAt)
            .HasDefaultValueSql("GETDATE()");
}

When it comes to the view I'm at a loss - I've read posts about overriding the save do I replace the async or am I supposed to slot it in somewhere here?

namespace Ceso.Pages.Peoples
{
    public class EditModel : PageModel
    {
        private readonly Ceso.Data.CesoContext _context;

        public EditModel(Ceso.Data.CesoContext context)
        {
            _context = context;
        }

        [BindProperty]
        public People People { get; set; } = default!;
        public DateTime UpdatedAt { get; set; }

        public async Task<IActionResult> OnGetAsync(int? id)
        {
            if (id == null || _context.People == null)
            {
                return NotFound();
            }

            var people =  await _context.People.FirstOrDefaultAsync(m => m.ID == id);

            if (people == null)
            {
                return NotFound();
            }

            People = people;

            return Page();
        }

        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            _context.Attach(People).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PeopleExists(People.ID))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return RedirectToPage("./Index");
        }

        private bool PeopleExists(int id)
        {
             return (_context.People?.Any(e => e.ID == id)).GetValueOrDefault();
        }
    } 
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • *I want to display the created date and the updated date* -- just displaying them (like FirstName etc. ) is not a problem. What's the *actual* problem? – Gert Arnold Aug 06 '23 at 09:31
  • The problem is I cannot display the updated date. If I update a record both the created date and the updated date are 00-00-00 – N Morrison Aug 08 '23 at 21:09
  • Please [edit] your question and explain *exactly* what you expect to happen with these date values when creating and updating records. – Gert Arnold Aug 09 '23 at 06:28
  • Found the answer in another post here https://stackoverflow.com/questions/38183021/how-to-automatically-populate-createddate-and-modifieddate – N Morrison Aug 09 '23 at 12:21
  • OK, I thought it should be something like that. You may also be interested in [this](https://stackoverflow.com/a/52021425/861716). – Gert Arnold Aug 09 '23 at 13:31
  • Thank you so much @GertArnold I have a different issue now - when ever I update a record the both UpdatedAt and CreatedAt are updated to DateTime.Now Are you able to point me in the right direction to keep the CreatedAt the same and just Update Modified at? – N Morrison Aug 09 '23 at 14:47

0 Answers0