public ActionResult Create(IEnumerable <NonHazardousWasteRecordWrapper> models) { List <NonHazardousWasteRecordWrapper> ss = new List <NonHazardousWasteRecordWrapper>(); double total = 0; foreach (var source in models) { List <NonHazardousWasteRecordWrapper> all = db.non_hazardous_record.Where(p => p.date == source.date).Select(p => new NonHazardousWasteRecordWrapper { id = p.id, waste_in = p.waste_in, waste_out = p.waste_out, id_type = p.id_type, date = p.date }).ToList(); if (all.Count() != 0) { total = all.Sum(p => p.waste_in).Value + source.waste_in.Value; foreach (NonHazardousWasteRecordWrapper a in all) { non_hazardous_record nn = new non_hazardous_record { id = a.id, date = a.date, waste_in = a.waste_in, waste_out = a.waste_out, id_type = a.id_type, recycle_rate = a.waste_out / total * 100 }; db.Entry(nn).State = EntityState.Modified; db.SaveChanges(); } } else { total = source.waste_in.Value; } var s = new non_hazardous_record { date = source.date, waste_in = source.waste_in, waste_out = source.waste_out, id_type = source.id_type, recycle_rate = source.waste_out / total * 100 }; db.non_hazardous_record.Add(s); db.SaveChanges(); TypeWasteWrapper tw = db.type_of_waste.Select(p => new TypeWasteWrapper { id = p.id, name = p.name }).Where(p => p.id == s.id_type).FirstOrDefault(); // store the product in the result ss.Add(new NonHazardousWasteRecordWrapper { id = s.id, type_name = tw, waste_in = s.waste_in, waste_out = s.waste_out, date = s.date }); } return(Json(ss.ToList())); }
public JsonResult BindingDropDown() { var a = db.type_of_waste; List <TypeWasteWrapper> result = new List <TypeWasteWrapper>(); foreach (type_of_waste type in a) { TypeWasteWrapper temp = new TypeWasteWrapper { id = type.id, name = type.name }; result.Add(temp); } return(Json(result, JsonRequestBehavior.AllowGet)); }