public ActionResult Create(StudentInfo model, string[] Month, decimal[] CourseFee, int[] ExamFee, DateTime[] AdmissionDate, IFormFile[] imgFile) { var oCatetory = (from c in ctx.StudentInfos where c.StudentName == model.StudentName.Trim() select c).FirstOrDefault(); if (oCatetory == null) { ctx.StudentInfos.Add(model); ctx.SaveChanges(); } else { model.StudentInfoId = oCatetory.StudentInfoId; } var listCourseFee = new List <FeeTbl>(); for (int i = 0; i < Month.Length; i++) { string imgPaths = ""; string imgPath = ProcessUploadFile(); if (imgFile[i] != null && imgFile[i].Length > 0) { //var fileName = Path.GetFileName(imgFile[i].FileName); //string fileLocation = Path.Combine( // Server.MapPath("~/uploads"), fileName); //imgFile[i].SaveAs(fileLocation); imgPaths = imgPath; } var newCourseFee = new FeeTbl(); newCourseFee.Month = Month[i]; newCourseFee.ExamFee = ExamFee[i]; newCourseFee.CourseFee = CourseFee[i]; newCourseFee.AdmissionDate = AdmissionDate[i]; newCourseFee.ImagePath = imgPaths; newCourseFee.ExamFee = ExamFee[i]; newCourseFee.StudentInfoId = model.StudentInfoId; listCourseFee.Add(newCourseFee); } ctx.FeeTbls.AddRange(listCourseFee); ctx.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult EditMultiple(StudentInfo model, int[] FeeTblId, string[] Month, decimal[] CourseFee, int[] ExamFee, DateTime[] AdmissionDate, IFormFile[] imgFile) { var listCourseFee = new List <FeeTbl>(); for (int i = 0; i < Month.Length; i++) { if (FeeTblId[i] > 0) { string imgPath = ""; if (imgFile[i] != null && imgFile[i].Length > 0) { //var fileName = Path.GetFileName(imgFile[i].FileName); //string fileLocation = Path.Combine( // Server.MapPath("~/uploads"), fileName); //imgFile[i].SaveAs(fileLocation); imgPath = "/uploads/" + imgFile[i].FileName; } int pid = FeeTblId[i]; var oCourseFee = ctx.FeeTbls.Where(w => w.FeeTblId == pid).FirstOrDefault(); if (oCourseFee != null) { oCourseFee.Month = Month[i]; oCourseFee.ExamFee = ExamFee[i]; oCourseFee.CourseFee = CourseFee[i]; oCourseFee.AdmissionDate = AdmissionDate[i]; oCourseFee.StudentInfoId = model.StudentInfoId; if (!string.IsNullOrEmpty(imgPath)) { //var fileName = Path.GetFileName(oCourseFee.ImagePath); //string fileLocation = Path.Combine(Server.MapPath("~/uploads"), fileName); //if (System.IO.File.Exists(fileLocation)) //{ // System.IO.File.Delete(fileLocation); //} } oCourseFee.ImagePath = imgPath == "" ? oCourseFee.ImagePath : imgPath; ctx.SaveChanges(); } } else if (!string.IsNullOrEmpty(Month[i])) { string imgPath = ""; if (imgFile[i] != null && imgFile[i].Length > 0) { //var fileName = Path.GetFileName(imgFile[i].FileName); //string fileLocation = Path.Combine( // Server.MapPath("~/uploads"), fileName); //imgFile[i].SaveAs(fileLocation); imgPath = "/uploads/" + imgFile[i].FileName; } var newCourseFee = new FeeTbl(); newCourseFee.Month = Month[i]; newCourseFee.ExamFee = ExamFee[i]; newCourseFee.CourseFee = CourseFee[i]; newCourseFee.AdmissionDate = AdmissionDate[i]; newCourseFee.ImagePath = imgPath; newCourseFee.ExamFee = ExamFee[i]; newCourseFee.StudentInfoId = model.StudentInfoId; ctx.FeeTbls.Add(newCourseFee); ctx.SaveChanges(); } } return(RedirectToAction("Index")); }