public ActionResult Edit([Bind(Include = "EmployeeID,EmployeeName,Title,TitleOfCourtesy,BirthDate,HireDate,Address,HomePhone,Extension,PhotoPath,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.Entry(employees).State = EntityState.Modified; 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("成功儲存修改")); }