in my controller i'm trying to check if a datetime value is being passed through in a put request but when i don't send a value for that datetime field it will reset the value in the database to 01/01/0001/ here is the code in my controller how i check if its empty or not.( a little bit of it ) (Check user.Age!)
[HttpPut("User/Edit/{userid}")]
public IActionResult Update(string token, int userid, [FromBody]User user)
{
bool RoleId = JWTValidator.RoleIDTokenValidation(token);
var edit = _context.users.Find(userid);
if (RoleId)
{
if (user.Name != null)
{
edit.Name = user.Name;
}
else
{
edit.Name = edit.Name;
}
if (user.LastName != null)
{
edit.LastName = user.LastName;
}
else
{
edit.LastName = edit.LastName;
}
if (user.Age != null)
{
edit.Age = user.Age;
}
else
{
edit.Age = edit.Age;
}
so if the value is null it won't update but i guess if you don't enter anything it will see it as 01/01/0001 so i tried comparing to it like this
if(user.Age == "01/01/0001")
but i can't compare type datetime to a string. and also i'm using validation like this
if (ModelState.IsValid)
{
_context.users.Update(edit);
_context.SaveChanges();
}
else
{
return BadRequest(ModelState);
}
and my model for Age is
[Display(Name = "Your Birthday")]
[DataType(DataType.Date, ErrorMessage = "Not a correct date format, MM/DD/YYYY please.")]
public DateTime Age { get; set; }
but if i send a put request with contents "Age": "test" it just gives me a 500 internal error instead of giving me the errormessage, am i handling that wrong?
Hope anyone could help me out.