// GET: /ExamAttendence/Create public ActionResult Create() { try { ViewBag.Class_Id = new SelectList(db.ClassInfoes.ToList(), "Id", "ClassName"); ViewBag.Faculty_Id = new SelectList(db.FacultyInfoes.ToList(), "Id", "FacultyName"); ViewBag.Present = new SelectList(PresentList(), "Id", "Text"); View_Attendence model = new View_Attendence(); model.StuList = new List<View_StudentPresent>(); string uname = User.Identity.Name; string Id = db.Users.Where(t => t.UserName == uname).SingleOrDefault().Id; ViewBag.UserId = Id; return View(model); } catch (Exception ex) { TempData["message"] = "<span class=\"color-red\">" + ex.InnerException.Message + "</span>"; } return RedirectToAction("Index"); }
public ActionResult Create(View_Attendence model, string SubjectId) { //if isCheck On that mean its check try { if (model.ClassId > 0 && model.FacultyId > 0 && model.SubjectId > 0 && model.StuList.Count > 0) { foreach (var item in model.StuList) { ExamAttendance newObj = new ExamAttendance(); if (item.IsCheck == "on") { newObj.Present = "Y"; } else { newObj.Present = "N"; } newObj.ClassId = model.ClassId; newObj.EntryBy = User.Identity.Name; newObj.EntryDate = DateTime.Now; newObj.FacultyId = model.FacultyId; newObj.SubjectId = model.SubjectId; newObj.StudentId = item.StudentId; //first try to get duplicate if found the update else save ExamAttendance prvObj = db.ExamAttendances.Where(t => t.ClassId == model.ClassId && t.FacultyId == model.FacultyId && t.SubjectId == model.SubjectId && t.StudentId == item.StudentId).SingleOrDefault(); if (prvObj != null) { prvObj.Present = newObj.Present; db.Entry(prvObj).State = EntityState.Modified; } else { db.ExamAttendances.Add(newObj); } } db.SaveChanges(); TempData["ok"] = "ok"; TempData["message"] = "<span class=\"color-green\">Successfully Saved!</span>"; } else { TempData["ok"] = ""; ViewBag.Message = "<span class=\"color-red\">One more validation Error.</span>"; } } catch (Exception ex) { TempData["message"] = "<span class=\"color-red\">" + ex.InnerException.Message+ "</span>"; } return RedirectToAction("Index"); }