public ActionResult Create(HocVien hocVien, HttpPostedFileBase hinhAnh) { try { if (ModelState.IsValid) { string path = ""; if (hinhAnh != null && hinhAnh.ContentLength > 0) { string extension = Path.GetExtension(hinhAnh.FileName); if (extension.Equals(".jpg") || extension.Equals(".png") || extension.Equals(".jpeg")) { path = Path.Combine(Server.MapPath("~/Img/HocVien/"), hinhAnh.FileName); hinhAnh.SaveAs(path); } hocVien.HinhAnh = hinhAnh.FileName; var _daoHocVien = new HocVienDao(); hocVien.NgayDangKy = DateTime.Now; int _maGiaoVien = _daoHocVien.Insert(hocVien); if (_maGiaoVien > 0) { if (hocVien.Email == null) { return(RedirectToAction("Index", "HocVien")); } else { string content = System.IO.File.ReadAllText(Server.MapPath("~/Assets/Email/HocVien.html")); content = content.Replace("{{TenHV}}", hocVien.TenHocVien.ToString()); content = content.Replace("{{NgaySinh}}", hocVien.NgaySinh.ToString()); content = content.Replace("{{SDT}}", hocVien.SDT.ToString()); content = content.Replace("{{DiaChi}}", hocVien.DiaChi.ToString()); content = content.Replace("{{NgayDangKy}}", hocVien.NgayDangKy.ToString()); new MailHelper().SendMail(hocVien.Email, "Chào mừng em đã tham gia vào đại gia đình Đan Thanh!", content); //SetAlert("") return(RedirectToAction("Index", "HocVien")); } } else { ModelState.AddModelError("", "Có lỗi khi thêm học viên"); } } } return(RedirectToAction("Index")); } catch { return(View()); } }
public ActionResult CreateDetails(HocVienDetails hocVienDetails, HttpPostedFileBase hinhAnhDetail) { try { if (ModelState.IsValid) { string path = ""; if (hinhAnhDetail != null && hinhAnhDetail.ContentLength > 0) { string extension = Path.GetExtension(hinhAnhDetail.FileName); if (extension.Equals(".jpg") || extension.Equals(".png") || extension.Equals(".jpeg")) { path = Path.Combine(Server.MapPath("~/Img/HocVien/"), hinhAnhDetail.FileName); hinhAnhDetail.SaveAs(path); } var _daoHocVien = new HocVienDao(); var _daoPhuHuynh = new PhuHuynhDao(); var hocVien = new HocVien(); hocVien.TenHocVien = hocVienDetails.HocVien.TenHocVien; hocVien.TaiKhoan = hocVienDetails.HocVien.TaiKhoan; hocVien.MatKhau = hocVienDetails.HocVien.MatKhau; hocVien.HinhAnh = hinhAnhDetail.FileName; hocVien.GioiTinh = hocVienDetails.HocVien.GioiTinh; hocVien.SDT = hocVienDetails.HocVien.SDT; hocVien.Email = hocVienDetails.HocVien.Email; hocVien.DiaChi = hocVienDetails.HocVien.DiaChi; hocVien.NgaySinh = hocVienDetails.HocVien.NgaySinh; hocVien.GhiChu = hocVienDetails.HocVien.GhiChu; hocVien.TrangThai = hocVienDetails.HocVien.TrangThai; hocVien.Nguon = hocVienDetails.HocVien.Nguon; // Thêm học viên int _maHocVien = _daoHocVien.Insert(hocVien); for (int i = 0; i < 2; i++) { var _phuHuynh = new PhuHuynh(); _phuHuynh.TenPhuHuynh = hocVienDetails.LstPhuHuynh[i].TenPhuHuynh; _phuHuynh.TenPhuHuynh = hocVienDetails.LstPhuHuynh[i].TenPhuHuynh; _phuHuynh.SDT = hocVienDetails.LstPhuHuynh[i].SDT; _phuHuynh.GioiTinh = hocVienDetails.LstPhuHuynh[i].GioiTinh; _phuHuynh.DiaChi = hocVienDetails.LstPhuHuynh[i].DiaChi; _phuHuynh.Email = hocVienDetails.LstPhuHuynh[i].Email; _phuHuynh.MaHocVien = _maHocVien; int _maPhuHuynh = _daoPhuHuynh.Insert(_phuHuynh); } if (_maHocVien > 0 && _maHocVien > 0) { if (hocVien.Email == null) { return(RedirectToAction("Index", "HocVien")); } else { string content = System.IO.File.ReadAllText(Server.MapPath("~/Assets/Email/HocVien.html")); content = content.Replace("{{TenHV}}", hocVien.TenHocVien.ToString()); content = content.Replace("{{NgaySinh}}", hocVien.NgaySinh.ToString()); content = content.Replace("{{SDT}}", hocVien.SDT.ToString()); content = content.Replace("{{DiaChi}}", hocVien.DiaChi.ToString()); content = content.Replace("{{NgayDangKy}}", hocVien.NgayDangKy.ToString()); new MailHelper().SendMail(hocVien.Email, "Chào mừng em đã tham gia vào đại gia đình Đan Thanh!", content); SetAlert("Thêm thành công", 1); return(RedirectToAction("Index", "HocVien")); } } else { ModelState.AddModelError("", "Có lỗi khi thêm chi tiết học viên"); } } } return(RedirectToAction("Index")); } catch { return(View()); } }