Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Date Time being set to 0001-01-01 12:01 AM

$
0
0

Hi, I have a row in a database.

When a record is created the date field is set to 0001-01-01 12:01 AM no matter what date is set to. Thanks,

Model

namespace ContosoUniversity.Models
{
    public class BloodSugar
    {
        public int ID { get; set; }

        public string ClientID { get; set; }

        [DataType(DataType.DateTime)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd hh:MM tt}", ApplyFormatInEditMode = true)]
        public DateTime DateTime { get; set; }

        [Required]
        public float Value { get; set; }

    }
}

Controller

        // GET: BloodSugar/Create
        public IActionResult Create()
        {
            return View();
        }

        // POST: BloodSugar/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Date,Value")] BloodSugar bloodsugar)
        {
            try {
            if (ModelState.IsValid)
            {
                _context.Add(bloodsugar);
                await _context.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            }
            catch
             (DbUpdateException  ex )
            {
                //Log the error (uncomment ex variable name and write a log.
                ModelState.AddModelError("", "Unable to save changes. " +
                    "Try again, and if the problem persists " +
                    "see your system administrator.");
            }

            return View(bloodsugar);
        }

View

<div class="form-group">
            <div>
                @Html.Label("Date Time", null,
            new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    <input type="datetime-local" />
                </div>
            </div>
        </div>

      


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>