public ActionResult AddStudent3() { ///Part 3 of the ADD STUDENT ACTION ///It is the final step of adding student ///The data will be retirve from the temp data stored in ADD_STUDENT_2 action try { AddStudent2ViewModel data = (AddStudent2ViewModel)TempData["reToAddStudent3"]; //get temp data and cast it into the model data //assemble the data into new view model AddStudent3ViewModel asvm = new AddStudent3ViewModel() { AddmissionNumber = data.AddmissionNumber, Name = data.Name, BForm = data.BForm, DOB = data.DOB, Gender = data.Gender, MonthlyFee = data.MonthlyFee, Prevnst = data.Prevnst, ParentId = data.ParentId, Class = data.Class }; return(View(asvm)); //return the view model to the view } catch (Exception ex) { return(Content(ex.Message)); } }
public ActionResult AddStudent3(AddStudent3ViewModel model) { ///Post method to get submitted data of form in ADD_STUDENT_3 VIEW try { ViewBag.Class = model.Class; //send class id to the view by using view bag ViewBag.ParentId = model.ParentId; //send parent id to the view by using view bag if (!ModelState.IsValid) { return(View()); } HttpCookie conString = Request.Cookies.Get("rwxgqlb"); //getting encrypted connection string from the cookie Student s; //an object of Student type to enter a new student into database try { if (model.BForm == null && model.Prevnst == null) { s = new Student(model.Name, model.AddmissionNumber, model.DOB, DateTime.Now, model.MonthlyFee, model.Gender, model.ParentId, model.Section, Cryptography.Decrypt(conString.Value)); } else if (model.BForm == null && model.Prevnst != null) { s = new Student(model.Name, model.AddmissionNumber, model.DOB, DateTime.Now, model.MonthlyFee, model.Gender, model.ParentId, model.Section, Cryptography.Decrypt(conString.Value), "", model.Prevnst); } else if (model.BForm != null && model.Prevnst == null) { s = new Student(model.Name, model.AddmissionNumber, model.DOB, DateTime.Now, model.MonthlyFee, model.Gender, model.ParentId, model.Section, Cryptography.Decrypt(conString.Value), model.BForm, ""); } else { s = new Student(model.Name, model.AddmissionNumber, model.DOB, DateTime.Now, model.MonthlyFee, model.Gender, model.ParentId, model.Section, Cryptography.Decrypt(conString.Value), model.BForm, model.Prevnst); } } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); return(View()); } string returnAction = (string)TempData["returnAction"]; //get path to action to which the controller will be redirected if (returnAction != "" || (returnAction != null && returnAction != "")) { TempData.Remove("returnAction"); //removes the temp data of return action return(RedirectToAction(returnAction, new { pId = s.Parent.ParentId })); //redirect to the action with the parent id } //showing the details of newly added student StudentDetailsViewModel savm = new StudentDetailsViewModel { Id = s.StudentId, BForm = s.BFormNumber, Class = s.Section.Class.Name, Name = s.Name, Section = s.Section.Name, AdmissionNumber = s.AdmissionNumber, DOA = s.DateOfAdmission.ToLongDateString(), Age = s.Age.ToString(), Fee = decimal.Round(s.MonthlyFee).ToString(), FName = s.Parent.FatherName, Gender = s.Gender + "", PrevInst = s.PreviousInstitute, RollNumber = s.RollNumber.ToString() }; ViewBag.Success = true; //flag to show successful operation return(View("ViewStudentDetails", savm)); //show the view student details view with view model having the student's details } catch (Exception ex) { return(Content(ex.Message)); } }