public ActionResult Create(StudentCreateVM studentmodel, FormCollection collection) { if (ModelState.IsValid) { var student = StudentVM.ToStudent(studentmodel); student.Password = studentmodel.Password; string courseIdstring = collection["Course"]; string[] courseArr = courseIdstring.Split(','); if (courseArr.Length > 5) { //throw new Exception("Cannot Select more than 5 course"); ModelState.AddModelError("", "Cannot Select more than 5 course"); List <string> gender = new List <string>(); gender.Add("Female"); gender.Add("Male"); var genderlist = new SelectList(gender); ViewBag.Gender = genderlist; List <Course> courses = courseservice.GetAll(); ViewBag.courses = new MultiSelectList(courses, "Id", "CourseName"); return(View()); } else { foreach (string str in courseIdstring.Split(',')) { Course c = courseservice.GetById(Convert.ToInt32(str)); student.Courses.Add(c); } stdManagementService.Add(student); return(RedirectToAction("Login", "Account")); } } return(View()); }