Ana H

Ana H

  • NA
  • 75
  • 418

Compare Json subitem date with current datetime in c#view

Jan 23 2020 5:35 AM
Hi.. I am using azure cosmosdb database. I am displaying webpage using Cosmosdb data and c# MVC web application. This time database json filr have DateTime as subitem in Json. I need to compare it with current datetime to flag signal on view page. Please find attached source code.
 
I am getting error:
 
Object reference not set to an instance of an object.
 
if I use "DateTime myday [email protected];"
 
I get error:
 
Cannot implicitly convert type 'System.Web.Mvc.MvcHtmlString' to 'System.DateTime' if I use " DateTime myday = Html.DisplayFor(modelItem => item.LastContact.Ping);"
 
Can anybody help please.
 
My code is as follows:
 
JSON:
  1. "DataTypeId": 1,  
  2. "IPaddress""192.168.2.177",  
  3. "id""d1b81653-b7a5-4d79-85ea-79c01f43f747",  
  4. "PhoneNo""0417518324",  
  5. "LastContact": {  
  6. "EnvLog""2019-09-07T19:41:08",  
  7. "Ping""2020-01-09T12:10:09",  
  8. "SpdLog""2019-09-07T19:41:08"  
  9. },  
  10. "SpeedLog"true,  
  11. "DeviceModelId": 2,  
  12. "DisconnectEvents": 9,  
Model:
  1.     public class DeviceMap  
  2.     {  
  3.   
  4.         [JsonProperty(PropertyName = "id")]  
  5.         public string Id { getset; }  
  6.   
  7.         [JsonProperty(PropertyName = "IPaddress")]  
  8.         public string IPaddress { getset; }  
  9.   
  10.         [JsonProperty(PropertyName = "SpeedLog")]  
  11.         public bool SpeedLog { getset; }  
  12.   
  13.         [JsonProperty(PropertyName = "LastContact")]  
  14.         public Entry LastContact { getset; }  
  15.     }  
  16.   
  17.      public class Entry  
  18.     {  
  19.           
  20.         [JsonProperty(PropertyName = "EnvLog")]  
  21.         public DateTime EnvLogtime { getset; }  
  22.   
  23.         [JsonProperty(PropertyName = "Ping")]  
  24.         public DateTime Ping { getset; }  
  25.   
  26.         [JsonProperty(PropertyName = "SpdLog")]  
  27.         public DateTime SpdLog { getset; }  
  28.     }  
  29.     }  
Controller:
  1.     public class DeviceMapController : Controller  
  2.     {  
  3.         [ActionName("Index")]  
  4.         public async Task<ActionResult> Map1Async()  
  5.         {  
  6.             string empty = "Unknown";  
  7.             var items = await DocumentDBRepository<DeviceMap>.GetMap1Async(d => d.Id != null, d => d.Lat != empty);  
  8.             if (Convert.ToInt32(User.GroupId()) != 200)   
  9.                 {  
  10.                     items = items.Where(d => d.GroupId == Convert.ToInt32(User.GroupId()));  
  11.             }  
  12.             return View(items);  
  13.         }  
  14.     }  
View:
  1. @foreach (var item in Model)  
  2. {  
  3. if ((@item.IPaddress == "192.168.2.14") || (@item.IPaddress == "192.168.2.13") || (@item.IPaddress == "192.168.2.15") || (@item.IPaddress == "192.168.2.16")) { }  
  4. else  
  5. {  
  6. //SideId string  
  7. string siteId = null;  
  8. string newsiteId = null;  
  9. if (item.IPaddress != "0")  
  10. {  
  11. int index = item.IPaddress.IndexOf('.') + 1;  
  12. int space = item.IPaddress.IndexOf('.', index + 1);  
  13. int space2 = item.IPaddress.IndexOf('.', space + 1);  
  14. int end = item.IPaddress.Length;  
  15. if (index > 0) { siteId = item.IPaddress.Substring(space + 1, end - space - 1); newsiteId = siteId.Replace('.''-'); }  
  16. }  
  17. // string Cutoff =Html.DisplayFor(modelItem => item.LastContact.Ping).ToString();  
  18. // DateTime day = DateTime.Parse(Cutoff.Value,"dd/MM/yyyy HH:mm:ss", CultureInfo.CurrentCulture);  
  19. DateTime myday =Html.DisplayFor(modelItem => item.LastContact.Ping);  
  20. DateTime now = DateTime.Now;  
  21. DateTime yesterday = now.AddDays(-7);  
  22. DateTime d;  
  23. // DateTime.TryParseExact(myday,@"dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal,out d);  
  24.   
  25. // if (Html.DisplayFor(modelItem => item.LastContact.Ping) != null) { d = DateTime.Parse(myday, "dd/MM/yyyy HH:mm:ss", CultureInfo.CurrentCulture); } else { myday = DateTime.ParseExact("08/04/2000 14:00:02", "dd/MM/yyyy HH: mm:ss", CultureInfo.CurrentCulture); }  
  26. string a = @item.Name;  
  27. <tr>  
  28. <td>  
  29. @if (a.Length > 50) { a = a.Substring(0, 50); }  
  30. @if (item.GroupId != 82)  
  31. {  
  32. if (myday <= DateTime.Now.AddDays(-7))  
  33. {<h4>@myday</h4>  
  34. <img src="/Content/Images/trafficlight-red2.png" width="20" height="20"><a href="@Url.Action("DevicePage", "Device", new { item.DeviceId })"><h6>@a..</h6></a>  
  35. }  
  36. else  
  37. {<img src="/Content/Images/trafficlight-green1.png" width="20" height="20"><a href="@Url.Action("DevicePage", "Device", new { item.DeviceId })"><h6>@a..</h6></a>  
  38. }  
  39. }  
  40. else  
  41. {  
  42. if (myday <= DateTime.Now.AddDays(-7))  
  43. {  
  44. <img src="/Content/Images/trafficlight-red2.png" width="20" height="20"><a href="@Url.Action("Form", "TARP", new { item.DeviceId })"><h6>@a..</h6></a>  
  45. }  
  46. else  
  47. {<img src="/Content/Images/trafficlight-green1.png" width="20" height="20"><a href="@Url.Action("Form", "TARP", new { item.DeviceId })"><h6>@a..</h6></a>  
  48. }  
  49. }  
  50. @if (item.IPaddress != "0")  
  51. {<p>Site Id: @newsiteId;</p>}  
  52. @if (item.LastPing != null)  
  53. {  
  54. <p>  
  55. Last Contact: @Html.DisplayFor(modelItem => item.LastContact.Ping), @Html.DisplayFor(modelItem => item.BatVoltage)V  
  56. </p>}

Answers (4)