コード例 #1
0
        public ActionResult Input(Employee model, string[] Roles, HttpPostedFileBase uploadFile)
        {
            if (string.IsNullOrEmpty(model.LastName))
            {
                ModelState.AddModelError("LastName", "LastName Expected");
            }
            if (string.IsNullOrEmpty(model.FirstName))
            {
                ModelState.AddModelError("FirstName", "FirstName Expected");
            }
            if (string.IsNullOrEmpty(model.Title))
            {
                ModelState.AddModelError("Title", "Title Expected");
            }
            if (string.IsNullOrEmpty(model.Email))
            {
                ModelState.AddModelError("Email", "Email Expected");
            }
            if (model.EmployeeID == 0)
            {
                if (CatalogBLL.CheckExist(model.Email) == 1)
                {
                    ModelState.AddModelError("Email", "Email Existed");
                }
            }

            if (string.IsNullOrEmpty(model.Password))
            {
                ModelState.AddModelError("Password", "Password Expected");
            }
            if (Roles != null)
            {
                for (int i = 1; i < Roles.Length; i++)
                {
                    model.Roles += "," + Roles[i];
                }
            }
            else
            {
                model.Roles = WebUserRoles.ANONYMOUS;
            }
            DateTime birthDate   = model.BirthDate;
            var      datestring  = "01/01/0001";
            var      time1       = "01/01/2002";
            var      time2       = "01/01/1900";
            var      datecurrent = DateTime.Now;
            DateTime date        = DateTime.Parse(datestring, System.Globalization.CultureInfo.InvariantCulture);

            if (birthDate == date)
            {
                ModelState.AddModelError("BirthDate", "BirthDate Expected");
            }

            DateTime hireDate = model.HireDate;

            if (hireDate == date)
            {
                ModelState.AddModelError("HireDate", "HireDate Expected");
            }

            //birthdate từ 1990 đên 2002
            if (DateTime.Compare(birthDate, DateTime.Parse(time2)) < 0)
            {
                ModelState.AddModelError("BirthDate", "BirthDate Expected");
            }
            if (DateTime.Compare(birthDate, DateTime.Parse(time1)) > 0)
            {
                ModelState.AddModelError("BirthDate", "BirthDate Expected");
            }

            if (DateTime.Compare(hireDate, datecurrent) > 0)
            {
                ModelState.AddModelError("hireDate", "BirthDate Expected");
            }

            if (string.IsNullOrEmpty(model.Country))
            {
                model.Country = "";
            }

            if (string.IsNullOrEmpty(model.Address))
            {
                model.Address = "";
            }
            if (string.IsNullOrEmpty(model.City))
            {
                model.City = "";
            }
            if (string.IsNullOrEmpty(model.Country))
            {
                model.Country = "";
            }
            if (string.IsNullOrEmpty(model.HomePhone))
            {
                model.HomePhone = "";
            }
            if (string.IsNullOrEmpty(model.Notes))
            {
                model.Notes = "";
            }
            if (string.IsNullOrEmpty(model.PhotoPath))
            {
                model.PhotoPath = "";
            }
            //if (string.IsNullOrEmpty(model.Password))
            //    model.Password = model.Password;


            //upload ảnh
            if (uploadFile != null)
            {
                string folder   = Server.MapPath("~/Images/Uploads");
                string fileName = $"{DateTime.Now.Ticks}{Path.GetExtension(uploadFile.FileName)}";
                //  string fileName = Guid.NewGuid() + uploadFile.FileName;
                string filePath = Path.Combine(folder, fileName);
                uploadFile.SaveAs(filePath);
                model.PhotoPath = fileName;
            }


            if (!ModelState.IsValid)
            {
                ViewBag.Title = model.EmployeeID == 0 ? "Create new Employee" : "Edit Employee";
                return(View(model));
            }
            //Lưu vào DB
            //TODO: Lưu dữ liệu vao DB


            if (model.EmployeeID == 0)
            {
                CatalogBLL.AddEmployee(model);
            }
            else
            {
                CatalogBLL.UpdateEmployee(model);
            }
            return(RedirectToAction("Index"));
        }