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:
- "DataTypeId": 1,
- "IPaddress": "192.168.2.177",
- "id": "d1b81653-b7a5-4d79-85ea-79c01f43f747",
- "PhoneNo": "0417518324",
- "LastContact": {
- "EnvLog": "2019-09-07T19:41:08",
- "Ping": "2020-01-09T12:10:09",
- "SpdLog": "2019-09-07T19:41:08"
- },
- "SpeedLog": true,
- "DeviceModelId": 2,
- "DisconnectEvents": 9,
Model:
- public class DeviceMap
- {
- [JsonProperty(PropertyName = "id")]
- public string Id { get; set; }
- [JsonProperty(PropertyName = "IPaddress")]
- public string IPaddress { get; set; }
- [JsonProperty(PropertyName = "SpeedLog")]
- public bool SpeedLog { get; set; }
- [JsonProperty(PropertyName = "LastContact")]
- public Entry LastContact { get; set; }
- }
- public class Entry
- {
- [JsonProperty(PropertyName = "EnvLog")]
- public DateTime EnvLogtime { get; set; }
- [JsonProperty(PropertyName = "Ping")]
- public DateTime Ping { get; set; }
- [JsonProperty(PropertyName = "SpdLog")]
- public DateTime SpdLog { get; set; }
- }
- }
Controller:
- public class DeviceMapController : Controller
- {
- [ActionName("Index")]
- public async Task
Map1Async() - {
- string empty = "Unknown";
- var items = await DocumentDBRepository
.GetMap1Async(d => d.Id != null, d => d.Lat != empty); - if (Convert.ToInt32(User.GroupId()) != 200)
- {
- items = items.Where(d => d.GroupId == Convert.ToInt32(User.GroupId()));
- }
- return View(items);
- }
- }
View:
- @foreach (var item in Model)
- {
- 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")) { }
- else
- {
- //SideId string
- string siteId = null;
- string newsiteId = null;
- if (item.IPaddress != "0")
- {
- int index = item.IPaddress.IndexOf('.') + 1;
- int space = item.IPaddress.IndexOf('.', index + 1);
- int space2 = item.IPaddress.IndexOf('.', space + 1);
- int end = item.IPaddress.Length;
- if (index > 0) { siteId = item.IPaddress.Substring(space + 1, end - space - 1); newsiteId = siteId.Replace('.', '-'); }
- }
- // string Cutoff =Html.DisplayFor(modelItem => item.LastContact.Ping).ToString();
- // DateTime day = DateTime.Parse(Cutoff.Value,"dd/MM/yyyy HH:mm:ss", CultureInfo.CurrentCulture);
- DateTime myday =Html.DisplayFor(modelItem => item.LastContact.Ping);
- DateTime now = DateTime.Now;
- DateTime yesterday = now.AddDays(-7);
- DateTime d;
- // DateTime.TryParseExact(myday,@"dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal,out d);
- // 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); }
- string a = @item.Name;
- @if (a.Length > 50) { a = a.Substring(0, 50); }
- @if (item.GroupId != 82)
- {
- if (myday <= DateTime.Now.AddDays(-7))
- {
@myday
"/Content/Images/trafficlight-red2.png" width="20" height="20">"@Url.Action("DevicePage", "Device", new { item.DeviceId })">
@a..
- }
- else
- {
"/Content/Images/trafficlight-green1.png" width="20" height="20">"@Url.Action("DevicePage", "Device", new { item.DeviceId })">
@a..
- }
- }
- else
- {
- if (myday <= DateTime.Now.AddDays(-7))
- {
"/Content/Images/trafficlight-red2.png" width="20" height="20">"@Url.Action("Form", "TARP", new { item.DeviceId })">
@a..
- }
- else
- {
"/Content/Images/trafficlight-green1.png" width="20" height="20">"@Url.Action("Form", "TARP", new { item.DeviceId })">
@a..
- }
- }
- @if (item.IPaddress != "0")
- {
Site Id: @newsiteId;
}- @if (item.LastPing != null)
- {
- Last Contact: @Html.DisplayFor(modelItem => item.LastContact.Ping), @Html.DisplayFor(modelItem => item.BatVoltage)V
- }
4 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions01ForumsTop experts
Arun PottiAnswers in the forums20answers02
Tahir AnsariAnswers in the forums5answers03
Kamlesh KumarAnswers in the forums5answers04
Sam HobbsAnswers in the forums4answers05
Željko PerićAnswers in the forums3answersFull leaderboard →
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Laxmidhar SahooRank #383 · 6.6K reputationPosted Jan 23, 2020, 5:48 AM EST
Ana H75 reputationPosted Jan 23, 2020, 6:17 AM EST
Laxmidhar SahooRank #383 · 6.6K reputationPosted Jan 24, 2020, 5:52 AM EST
Laxmidhar SahooRank #383 · 6.6K reputationPosted Jan 25, 2020, 7:03 AM EST