//Call when new adding public StudentViewModel() { TabTitle = "Add Students"; ButtonName = "Add"; SchoolObjContext context = new SchoolObjContext(); AllStandards = context.Standards.ToList(); SubmitStudentCommand = new RelayCommand(x => { if (!CheckCanSubmit()) { MessageBox.Show("Fill all the field properly", "Validation Error", MessageBoxButton.OK, MessageBoxImage.Error); //CanShowResult = false; return; } SchoolObjContext ctx = new SchoolObjContext(); //EntityDatabase.DomainClasses.Standard stan = ctx.Standards.Where(s => s.StandardId == this.SelectedStandard.StandardId).FirstOrDefault(); ctx.Students.Add(new Student() { FirstName = this.FirstName, MiddleName = this.MiddleName, LastName = this.LastName, Age = this.Age, City = this.City, StandardId = this.SelectedStandard.StandardId, Doc = this.DocFile, DocType = this.DocFileType }); if (ctx.SaveChanges() > 0) { MessageBox.Show("Student Registered Successfully", "Registered !", MessageBoxButton.OK, MessageBoxImage.Information); //CanShowResult = true; MainViewModel.RefreshView("Show Students"); } else { //CanShowResult = false; MessageBox.Show("Student Registration Failed", "Registration Failed !", MessageBoxButton.OK, MessageBoxImage.Error); } this.FirstName = MiddleName = LastName = ""; this.Age = 0; this.City = ""; this.SelectedStandard = ctx.Standards.FirstOrDefault(); this.DocFile = null; OnPropertyChanged("SelectedStandard"); }); //ShowResultCommand = new RelayCommand(x => //{ // SubmitStudentCommand.Execute(null); // if (CanShowResult) // MainViewModel.Tabs.Add(new ResultViewModel(context.Students.OrderByDescending(s => s.StudentId).FirstOrDefault<Student>().StudentId)); //}); UserControl = new UserControls.Student { DataContext = this }; }
//Call when Editing public StudentViewModel(int Id) { TabTitle = "Edit Students"; ButtonName = "Save Changes"; SchoolObjContext context = new SchoolObjContext(); if (Id > 0) { using (var Context = new SchoolObjContext()) { Student obj = Context.Students.Where(s => s.StudentId == Id).FirstOrDefault(); AllStandards = context.Standards.Where(s => s.StandardId == obj.StandardId).ToList(); if (obj != null) { this.FirstName = obj.FirstName; this.MiddleName = obj.MiddleName; this.LastName = obj.LastName; this.Age = obj.Age; this.City = obj.City; this.DocFileType = obj.DocType; this.DocFile = obj.Doc; SelectedStandard = Context.Standards.Where(s => s.StandardId == obj.StandardId).FirstOrDefault(); for (int i = 0; i < AllStandards.Count; i++) { if (AllStandards[i].StandardId == SelectedStandard.StandardId) { StdIndex = i; break; } } } } } //Update edited student SubmitStudentCommand = new RelayCommand(x => { if (!CheckCanSubmit()) { MessageBox.Show("Fill all the field properly", "Validation Error", MessageBoxButton.OK, MessageBoxImage.Error); //CanShowResult = false; return; } //else // CanShowResult = true; EntityDatabase.DomainClasses.Student stud = context.Students.Where(s => s.StudentId == Id).FirstOrDefault(); if (stud != null) { stud.FirstName = this.FirstName; stud.MiddleName = this.MiddleName; stud.LastName = this.LastName; stud.Age = this.Age; stud.City = this.City; stud.StandardId = this.SelectedStandard.StandardId; stud.Doc = this.DocFile; stud.DocType = this.DocFileType; if (context.SaveChanges() > 0) { MainViewModel.RefreshView("Show Students"); MainViewModel.RefreshView("Result Report"); MessageBox.Show("Student Edited Successfully", "Edited !", MessageBoxButton.OK, MessageBoxImage.Information); //CanShowResult = true; } //else //{ // MessageBox.Show("Student not Edited", "Failed !", MessageBoxButton.OK, MessageBoxImage.Error); // CanShowResult = false; //} OnPropertyChanged("SelectedStandard"); this.CloseTabCommand.Execute(null); MainViewModel.Tabs.Add(new ShowAllStudentViewModel()); } }); //ShowResultCommand = new RelayCommand(x => //{ // this.SubmitStudentCommand.Execute(null); // if (CanShowResult) // MainViewModel.Tabs.Add(new ViewModels.ResultViewModel(Id)); //}); UserControl = new UserControls.Student { DataContext = this }; }