public TeacherAllocationViewModel AddAndUpdateTeacherAllocation(TeacherAllocationViewModel vmModel) { try { if (vmModel.ID > 0) { var record = _db.TeacherAllocations.OrderByDescending(x => x.ID).Where(x => x.ID == vmModel.ID).FirstOrDefault(); record.ClassMasterID = vmModel.ClassMasterID; record.TeacherMasterID = vmModel.TeacherMasterID; record.StudentMasterID = vmModel.StudentMasterID; _db.SaveChanges(); } else { TeacherAllocation _TeacherAllocation = new TeacherAllocation(); _TeacherAllocation.ClassMasterID = vmModel.ClassMasterID; _TeacherAllocation.TeacherMasterID = vmModel.TeacherMasterID; _TeacherAllocation.StudentMasterID = vmModel.StudentMasterID; _db.TeacherAllocations.Add(_TeacherAllocation); _db.SaveChanges(); vmModel.ID = _TeacherAllocation.ID; } } catch (Exception ex) { } return(vmModel); }
protected void Submit_Click(object sender, EventArgs e) { TeacherAllocationViewModel vmModel = new TeacherAllocationViewModel(); vmModel.ClassMasterID = Convert.ToInt32(ClassMasterID.Text); vmModel.TeacherMasterID = Convert.ToInt32(TeacherMasterID.Text); vmModel.StudentMasterID = Convert.ToInt32(StudentMasterID.Text); if (HiddenField1.Value != "") { vmModel.ID = Convert.ToInt32(HiddenField1.Value); } vmModel = iTeacherAllocationService.AddAndUpdateTeacherAllocation(vmModel); if (vmModel.ID > 0) { Response.Write("<script>alert('Record saved successfully')</script>"); Response.Redirect("TeacherAllocation.aspx"); } bindGrid(); }