Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...