private string CreateStudentRegistrationNo(Student student) { int id = db.Students.Count(s => (s.DepartmentId == student.DepartmentId) && (s.Date.Year == student.Date.Year)) + 1; Department aDepartment = db.Departments.Where(d => d.DepartmentId == student.DepartmentId).FirstOrDefault(); string registrationId = aDepartment.Code + "-" + student.Date.Year + "-"; string addZero = ""; int len = 3 - id.ToString().Length; for (int i = 0; i < len; i++) { addZero = "0" + addZero; } return registrationId + addZero + id; }
public ActionResult Create( Student student) { if (ModelState.IsValid) { Regex phoneFormate = new Regex(@"^([0-9\(\)\/\+ \-]*)$"); if (phoneFormate.IsMatch(student.ContactNo)) { student.RegistrationId = CreateStudentRegistrationNo(student); ViewBag.registrationId = student.Name + " Registration ID: " + student.RegistrationId; db.Students.Add(student); db.SaveChanges(); RedirectToAction("Create"); } else { TempData["Message"] = "Contact format is not valid"; RedirectToAction("Create"); } } ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "Code", student.DepartmentId); return View(student); }
public ActionResult Edit( Student student) { if (ModelState.IsValid) { db.Entry(student).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "Code", student.DepartmentId); return View(student); }