Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

c# - How to initialize DateTime property to show Date.Now

I am trying to set DateTime Field to display current time and date.

 public DateTime Date { get; set;  }

What I try so far to pass in setter Date.Now but doesn't work. I am asking because I need to display DateTime.Now in View but this items should be hidden from User. User can only see DateTime but can not Edit. Also in Controller I use something like but doesn't work

 DateTime Date = DateTime.Now;

Any idea where I made mistake and how to fix this issues ?

UPDATE

Here is my Controller

 public NotesController(ApplicationDbContext db)
        {
            _db = db;
        }

 public IActionResult Index()
        {
            IEnumerable<Notes> notes = _db.Notes.Include(u => u.Patient);
            return View(notes);
        }

        //Upsert GET
        public IActionResult Upsert(int? Id)
        {
            DateTime Date = DateTime.Now;
            
            NotesVM notesVM = new NotesVM()
            {
                Notes = new Notes(),

                PatientSelectList = _db.Patients.Select(i => new SelectListItem
                {
                    Text = i.FirstName + i.LastName,
                    Value = i.Id.ToString()
                })

            };

           

            Notes notes = new Notes();
            if (Id == null)
            {
                // this is for create
                return View(notesVM);

            }
            else
            {
                // this is for edit
                notesVM.Notes = _db.Notes.Find(Id);
                if (notesVM.Notes == null)
                {
                    return NotFound();
                }
                return View(notesVM);
            }
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Upsert(NotesVM notesVM)
        {
            if (ModelState.IsValid)
            {

                if (notesVM.Notes.Id == 0)
                {
                    //Creating
                    _db.Notes.Add(notesVM.Notes);
                }
                else
                {
                    //Updating
                    _db.Notes.Update(notesVM.Notes);
                }

                _db.SaveChanges();
                return RedirectToAction("Index");
            }

            notesVM.PatientSelectList = _db.Patients.Select(i => new SelectListItem
            {
                Text = i.FirstName + i.LastName,
                Value = i.Id.ToString()
            });

            return View(notesVM);
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you have a constructor for your controller you can set the date property like this:

 public NotesController(ApplicationDbContext db)
 {
      _db = db;
      Date = DateTime.Now;
 }

updated my answer now that i have seen your constructor.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...