Пример #1
0
        public ActionResult Create([Bind(Include = "EmployeeID,EmployeeName,Title,TitleOfCourtesy,BirthDate,HireDate,Address,HomePhone,PhotoPath,Extension,Notes,ManagerID,Salary")] Employees employees, HttpPostedFileBase file)
        {
            if (file != null)
            {
                if (file.ContentLength > 0)
                {
                    //判斷檔案名稱
                    // 取得副檔名
                    string fileType = file.FileName.Split('.').Last().ToUpper();
                    if (!(fileType.Equals("JPG") || fileType.Equals("PNG") || fileType.Equals("GIF")))
                    {
                        TempData["Message"] = "只接受jpg,png,gif等圖片類型";
                        return(View(employees));
                    }
                    TempData["Message"] = "";
                    var fileName = Path.GetFileName(file.FileName);
                    var path     = Path.Combine(Server.MapPath("~/UploadedImages"), fileName);
                    Session["path"] = fileName;//儲存檔案名稱
                    file.SaveAs(path);
                }
            }

            if (ModelState.IsValid)
            {
                employees.PhotoPath = Session["path"].ToString();
                db.Employees.Add(employees);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(employees));
        }
        //儲存修改
        public ActionResult SaveEdit(int id, string name, string title, string titlec, string bdate, string hdate, string address, string hphone, string ex, string photopath, string notes, int mgid, int salary)
        {
            DateTime birthdate = DateTime.Parse(bdate);
            DateTime hiredate  = DateTime.Parse(hdate);

            using (NorthwindChineseEntities db = new NorthwindChineseEntities())
            {
                Employees e = db.Employees.Find(id);
                e.EmployeeName    = name;
                e.Title           = title;
                e.TitleOfCourtesy = titlec;
                e.BirthDate       = birthdate;
                e.HireDate        = hiredate;
                e.Address         = address;
                e.HomePhone       = hphone;
                e.Extension       = ex;
                e.PhotoPath       = photopath;
                e.Notes           = notes;
                e.ManagerID       = mgid;
                e.Salary          = salary;

                db.Entry(e).State = EntityState.Modified;
                db.SaveChanges();
            }

            return(Json("成功儲存修改"));
        }
 //刪除
 public ActionResult Delete(string id)
 {
     using (NorthwindChineseEntities db = new NorthwindChineseEntities())
     {
         Employees e = db.Employees.Find(id);
         db.Employees.Remove(e);
         db.SaveChanges();
     }
     return(Json("成功刪除"));
 }
        //儲存新增
        public ActionResult New(string name, string title, string titlec, string bdate, string hdate, string address, string hphone, string ex, string photopath, string notes, int mgid, int salary)
        {
            using (NorthwindChineseEntities db = new NorthwindChineseEntities())
            {
                DateTime  birthdate = DateTime.Parse(bdate);
                DateTime  hiredate  = DateTime.Parse(hdate);
                Employees newem     = new Employees()
                {
                    EmployeeName = name, Title = title, TitleOfCourtesy = titlec, BirthDate = birthdate, HireDate = hiredate, Address = address, HomePhone = hphone, Extension = ex, PhotoPath = photopath, Notes = notes, ManagerID = mgid, Salary = salary
                };

                // if (ModelState.IsValid)
                // {
                db.Employees.Add(newem);
                db.SaveChanges();
                //}
                // string birthdate = DateTime.Parse(bdate).ToString("yyyy/MM/dd");
                // string hiredate = DateTime.Parse(hdate).ToString("yyyy/MM/dd");
            }

            return(Json("新增成功"));
        }