示例#1
0
        public async Task <ActionResult> Create(NhanVienViewModel nhanVien, HttpPostedFileBase avatar)
        {
            if (avatar != null && avatar.ContentLength > 0)
            {
                try
                {
                    string path = Path.Combine(Server.MapPath("~/Content/image/user"),
                                               Path.GetFileName(avatar.FileName));
                    avatar.SaveAs(path);
                    nhanVien.avatar = avatar.FileName;
                }
                catch (Exception ex)
                {
                    ViewBag.Message = "ERROR:" + ex.Message.ToString();
                }
            }
            else
            {
                nhanVien.avatar = "default.png";
            }

            try
            {
                await _nhanVienKhoBus.Create(nhanVien);

                SetAlert("Đã thêm nhân viên thành công!!!", "success");
            }
            catch
            {
                TempData["nhanVien"] = nhanVien;
                SetAlert("Đã xảy ra lỗi! Bạn hãy thêm lại", "error");
            }
            return(RedirectToAction("Index"));
        }
        public async Task <ActionResult> Create(NhanVienViewModel nhanVien, HttpPostedFileBase avatar)
        {
            if (avatar != null && avatar.ContentLength > 0)
            {
                try
                {
                    string path = Path.Combine(Server.MapPath("~/Content/image/user"),
                                               Path.GetFileName(avatar.FileName));
                    avatar.SaveAs(path);
                    nhanVien.avatar = avatar.FileName;
                }
                catch (Exception ex)
                {
                    ViewBag.Message = "ERROR:" + ex.Message.ToString();
                }
            }
            else
            {
                nhanVien.avatar = "default.png";
            }

            try
            {
                string content = System.IO.File.ReadAllText(Server.MapPath("~/Content/client/template/themnhanvien.html"));

                content = content.Replace("{{TenNhanVien}}", nhanVien.tenNhanVien);
                content = content.Replace("{{Username}}", nhanVien.userName);
                content = content.Replace("{{Password}}", nhanVien.password);

                String subject = "Thông tin từ cửa hàng BK Computer!!!";
                SentMail.Sent(subject, nhanVien.email, "*****@*****.**", "T12345678", content);

                await _nhanVienKhoBus.Create(nhanVien);

                SetAlert("Đã thêm nhân viên thành công!!!", "success");
            }
            catch
            {
                TempData["nhanVien"] = nhanVien;
                SetAlert("Đã xảy ra lỗi! Bạn hãy thêm lại", "error");
            }
            return(RedirectToAction("Index"));
        }