I am a newbie here. I have gone through similar questions here but none of them helped. I have the following in my ViewModel
:
public DateTime FromDate { get; set; }
public DateTime ToDate { get; set; }
When I run the code, in the View
after GET Method
, I am getting the default dates as: 01/01/0001
, in other words, null
/default value. I searched online and found out that I need to make these fields nullable
. Therefore, I changed the above code to:
public DateTime? FromDate { get; set; }
public DateTime? ToDate { get; set; }
Upon changing, I am getting the following error for both FromDate
and ToDate
:
Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)
What to do?
Edit:
[HttpPost]
public IActionResult Locate(...)
{
InventoryHistory history = new InventoryHistory();
...
...
history.FromDate = locationsViewModel.FromDate;
history.ToDate = locationsViewModel.ToDate;
...
...
_context.Add(history);
_context.SaveChanges();
return RedirectToAction("Details");
}