public ActionResult Home(HomeClientModelView SenorName) { using (ApplicationDbContext db = new ApplicationDbContext()) { string ClientEmail = Session["UserId"].ToString(); Resetings reset = new Resetings(); Senzors senzor = new Senzors(); try { senzor = db.senzors.FirstOrDefault(s => s.Name == SenorName.SenzorName); reset.SenzorsId = senzor.id; reset.BateryLevel = senzor.BateryLevel; reset.ResetDate = DateTime.Now; reset.HasChecked = true; db.resetings.Add(reset); db.SaveChanges(); } catch (Exception e) { ViewBag.Error = "Something went wrong finding the senzor"; } } return(Home()); }
public ActionResult Home() { using (ApplicationDbContext db = new ApplicationDbContext()) { string ClientEmail = Session["UserId"].ToString(); HomeClientModelView home = new HomeClientModelView(); Senzors senzor = new Senzors(); try { var client = db.client.FirstOrDefault(c => c.Email == ClientEmail); home.Email = ClientEmail; home.FirstName = client.FirstName; home.LastName = client.LastName; home.ImagePath = client.PathImage; home.senzors = db.senzors.Where(s => s.ClientId == client.id).ToList(); if (home.senzors.Count() > 0) { string KeySenzor = ""; foreach (var reset in home.senzors) { List <string> SenzorInfo = new List <string>(); Resetings reseting = new Resetings(); KeySenzor = reset.Name; //SenzorInfo.Add(reset.Name); SenzorInfo.Add(reset.BateryLevel.ToString()); try { // reseting = db.resetings.LastOrDefault(r => r.SenzorsId == reset.id); var getall = db.resetings.GroupBy(r => r.SenzorsId).Select(p => p.FirstOrDefault(w => w.id == p.Max(m => m.id))) .OrderBy(p => p.SenzorsId).ToList(); reseting = getall.Where(s => s.SenzorsId == reset.id).FirstOrDefault(); SenzorInfo.Add(reseting.BateryLevel.ToString()); SenzorInfo.Add(reseting.HasChecked.ToString()); } catch (Exception e) { SenzorInfo.Add("TBD"); SenzorInfo.Add("New Senzor"); } home.SenzorInfo.Add(KeySenzor, SenzorInfo); } } return(View(home)); } catch (Exception e) { ViewBag.Error = ""; } return(View(home)); } }