I'm having this weird problem when it comes to dates. I'm trying to get a value from mongodb using date. I know something happened that's why even if they have the same date, nothing is being returned. Here's the process:
I'm pulling the data from the database using an Id, now the record I'm getting has a date. This date is being displayed on my view. Here's how I do it.
Action:
public JsonResult GetDateSentList(string id)
{
var data = ApiHelper<List<SomeModel>>.Get("SomeController/SomeAction?id=" + id); //I created a helper for requests
var distinctData = data.Select(a => a.DateOfEvent).Distinct().ToList();
foreach (var item in distinctData)
{
var dateData = new SomeModel();
dateData = data.FirstOrDefault(a => a.DateOfEvent == distinctData[i]);
dateData.DateOfEventString = dateData.DateOfEvent.ToString(@"MM-dd-yyyy hh:mm", new CultureInfo("en-US"));
dateData.DateOfEventExactString = dateData.DateOfEvent.ToString(@"MM-dd-yyyy hh:mm:ss tt", new CultureInfo("en-US"));
}
return Json(listDistinctData);
}
As you can notice this is being accessed by an ajax call and the values here (date strings) are being displayed using js.
Here's the value I'm getting at this point: 6/30/2014 7:01:17 AM (this is now a string in html value)
Now, at a button press, I'm passing this same date string on an action and this action performs a request to a WebApi.
On the WebApi, the string is being converted to dateTime (request.DateSent) and at this point I'm trying to get the same record that I got on the first part using the date that I got on the record that I want to get.
x= someService.All().FirstOrDefault(c => c.DateOfEvent == request.DateSent);
I checked the record that I wanted to get, it has a DateTime value 6/30/2014 7:01:17 AM and the DateTime that I'm using to get the record that I wanted has the same DateTime value 6/30/2014 7:01:17 AM. Sadly, I keep on getting a null value. Something happened here that caused them to be not equal, but I can't trace it at all. Any ideas? Thanks!