public ActionResult Create([Bind(Include = "CourseId,CourseCode,CourseName,IsActive,UserDetailsId,CreatedDateTime")] Course course) { if (ModelState.IsValid) { if (string.IsNullOrEmpty(course.CourseCode) || string.IsNullOrEmpty(course.CourseName)) { ViewBag.Required = "Course Code & Course Name is Required!!!"; return(View()); } if (db.Courses.ToList().Exists(a => a.CourseCode == course.CourseCode)) { ViewBag.CodeExist = "This Course Code Already Exist!!!"; return(View()); } course.UserDetailsId = Convert.ToInt32(Session["UserDetailsId"]); course.CreatedDateTime = DateTime.Now; db.Courses.Add(course); db.SaveChanges(); ViewBag.Required = ""; ViewBag.CodeExist = ""; return(RedirectToAction("Index")); } ViewBag.UserDetailsId = new SelectList(db.UserDetails, "UserDetailsId", "UserName", course.UserDetailsId); return(View(course)); }
public ActionResult Create(int?TraineeId, string TraineeName, string TraineeImage, string FatherName, string MotherName, string Gender, string Age, string Address, string ContactNumber, string Email, string NationalIdCard, string TraineeCode, int?CourseId, int?BatchId, HttpPostedFileBase file) { //if (ModelState.IsValid) //{ TraineeCourseId,TraineeCode,TraineeId,CourseId,BatchId,Result,UserDetailsId // db.Trainees.Add(trainee); // db.SaveChanges(); // return RedirectToAction("Index"); //} //ViewBag.BatchId = new SelectList(db.Batches, "BatchId", "BatchCode", trainee.BatchId); //ViewBag.UserDetailsId = new SelectList(db.UserDetails, "UserDetailsId", "UserName", trainee.UserDetailsId); //return View(trainee); bool Status = false; string message = ""; var courseList = db.Courses.ToList(); if (ModelState.IsValid) { int totalTraining = db.TraineeCourses.Count(a => a.TraineeId == TraineeId); bool isThisCourseAlreadyDone = db.TraineeCourses.Any(a => a.TraineeId == TraineeId && a.CourseId == CourseId); if (CourseId == null) { ViewBag.Required = "PLease Select a Course"; ViewBag.Courses = courseList; return(View()); } if (BatchId == null) { ViewBag.Required = "PLease Select a Batch"; ViewBag.Courses = courseList; return(View()); } if (isThisCourseAlreadyDone) { ViewBag.Required = "This Trainee Already Done This Course"; ViewBag.Courses = courseList; return(View()); } if (totalTraining >= 3) { ViewBag.Required = "This Trainee Already Done 3(Three) Courses"; ViewBag.Courses = courseList; return(View()); } bool isThisCourseCodeAlreadyHas = db.TraineeCourses.Any(a => a.TraineeCode == TraineeCode); if (isThisCourseCodeAlreadyHas) { ViewBag.Required = "This Trainee Code Already Given"; ViewBag.Courses = courseList; return(View()); } #region Image var path = ""; if (file != null) { if (file.ContentLength > 0) { //for checking uploaded file is image or not if ((Path.GetExtension(file.FileName).ToLower() == ".jpg") || (Path.GetExtension(file.FileName).ToLower() == ".png") || (Path.GetExtension(file.FileName).ToLower() == ".gif") || (Path.GetExtension(file.FileName).ToLower() == ".jpeg")) { if (file.ContentLength > 307200) //300kb { ViewBag.SizeConflict = true; ViewBag.Courses = courseList; ViewBag.Required = "Maximum Picture Size 300 KB!!!"; return(View()); } string fileName = Path.GetFileNameWithoutExtension(file.FileName); string extention = Path.GetExtension(file.FileName); fileName = fileName + DateTime.Now.ToString("yymmssfff") + extention; path = "~/Content/TraineeImages/" + fileName; fileName = Path.Combine(Server.MapPath("~/Content/TraineeImages/"), fileName); file.SaveAs(fileName); ViewBag.UploadSuccess = true; } } #endregion Trainee aTrainee = new Trainee() { TraineeId = TraineeId == null ? 0 : (int)TraineeId, TraineeName = TraineeName, TraineeImage = path, FatherName = FatherName, MotherName = MotherName, Gender = Gender, Age = Convert.ToDouble(Age), Address = Address, ContactNumber = ContactNumber, Email = Email, NationalIdCard = NationalIdCard, UserDetailsId = Convert.ToInt32(Session["UserDetailsId"]), CreatedDateTime = DateTime.Now }; #region Email and NID already Exists or Not! var isExist = aTrainee.TraineeId >= 0?false:IsEmailExist(Email); if (isExist) { ViewBag.Required = "This Email Already Used"; ViewBag.Courses = courseList; //ModelState.AddModelError("EmailExist", "Email already exist"); return(View()); } var NIDExist = aTrainee.TraineeId >= 0 ? false : IsNIDExist(NationalIdCard); if (NIDExist) { ViewBag.Required = "This National Id Card Already Used"; ViewBag.Courses = courseList; //ModelState.AddModelError("NIDRepeat", "NID already exist"); return(View()); } var phoneExist = aTrainee.TraineeId >= 0 ? false : IsPhoneExist(ContactNumber); if (phoneExist) { ViewBag.Required = "This Contact Number Already Used"; ViewBag.Courses = courseList; //ModelState.AddModelError("NIDRepeat", "NID already exist"); return(View()); } TraineeCourse aTraineeCourse = new TraineeCourse() { TraineeCode = TraineeCode, TraineeId = TraineeId == null?0:(int)TraineeId, CourseId = CourseId == null ? 0 : (int)CourseId, BatchId = BatchId == null ? 0 : (int)BatchId, Result = "", UserDetailsId = Convert.ToInt32(Session["UserDetailsId"]), CreatedDateTime = DateTime.Now }; if (aTrainee.TraineeId == 0) { aTraineeCourse.Trainee = aTrainee; } #endregion db.TraineeCourses.Add(aTraineeCourse); db.SaveChanges(); ViewBag.Required = "Trainee Saved"; ViewBag.Courses = courseList; return(View()); } } else { message = "Invalid Request"; ViewBag.Courses = courseList; return(View()); } //ViewBag.BatchId = new SelectList(db.Batches, "BatchId", "BatchCode", trainee.BatchId); //ViewBag.UserDetailsId = new SelectList(db.UserDetails, "UserDetailsId", "UserName", trainee.UserDetailsId); ViewBag.Required = "Please Provide All Information"; ViewBag.Courses = courseList; return(View()); }
public ActionResult Create([Bind(Include = "BatchId,BatchCode,CourseId,StartDate,EndDate,Time,Instructor,CourseDetails,Duration,CourseFee,TotalSeat,Status,UserDetailsId,CreatedDateTime")] Batch batch) { if (ModelState.IsValid) { if (string.IsNullOrEmpty(batch.BatchCode)) { ViewBag.Required = "Batch Code is Required!!!"; return(View()); } if (db.Batches.ToList().Exists(a => a.BatchCode == batch.BatchCode)) { ViewBag.CodeExist = "This Batch Code Already Exist!!!"; return(View()); } if (string.IsNullOrEmpty(batch.Time)) { ViewBag.Required = "Time is Required!!!"; return(View()); } if (string.IsNullOrEmpty(batch.Instructor)) { ViewBag.Required = "Instructor is Required!!!"; return(View()); } try { Convert.ToDouble(batch.CourseFee); } catch { ViewBag.Required = "Valid Course Fee Required!!!"; return(View()); } try { Convert.ToInt32(batch.TotalSeat); } catch { ViewBag.Required = "Valid Total Seat Required!!!"; return(View()); } try { Convert.ToInt32(batch.Duration); } catch { ViewBag.Required = "Valid Duration Required!!!"; return(View()); } //batch.Duration = 0; batch.Status = ""; batch.UserDetailsId = Convert.ToInt32(Session["UserDetailsId"]); batch.CreatedDateTime = DateTime.Now; db.Batches.Add(batch); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "CourseCode", batch.CourseId); ViewBag.UserDetailsId = new SelectList(db.UserDetails, "UserDetailsId", "UserName", batch.UserDetailsId); return(View(batch)); }