public ActionResult AddOrEdit(Employee emp) { try { if (emp.ImageUpload != null) { string fileName = Path.GetFileNameWithoutExtension(emp.ImageUpload.FileName); string extension = Path.GetExtension(emp.ImageUpload.FileName); fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension; emp.ImagePath = "~/AppFiles/Images/" + fileName; emp.ImageUpload.SaveAs(Path.Combine(Server.MapPath("~/AppFiles/Images/"), fileName)); } using (jQueryAjaxDBEntities ent = new jQueryAjaxDBEntities()) { if (emp.Id == 0) { ent.Employees.Add(emp); ent.SaveChanges(); } else { ent.Entry(emp).State = EntityState.Modified; ent.SaveChanges(); } } return(Json(new { success = true, html = ViewToStringConverter.RenderRazorViewToString(this, "ViewAll", GetAllEmployee()), message = "Submitted Successfully" }, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { return(Json(new { success = false, message = ex.Message }, JsonRequestBehavior.AllowGet)); } }
public ActionResult AddOrEdit(Employee emp) { try { if (emp.ImageUpload != null) { string fileName = Path.GetFileNameWithoutExtension(emp.ImageUpload.FileName); string extension = Path.GetExtension(emp.ImageUpload.FileName); fileName = fileName + DateTime.Now.ToString("yymmssff") + extension; emp.ImagePath = "~/AppFiles/Images/" + fileName; emp.ImageUpload.SaveAs(Path.Combine(Server.MapPath("~/AppFiles/Images/"), fileName)); } using (jQueryAjaxDBEntities db = new jQueryAjaxDBEntities()) { if (emp.EmployeeID == 0) { db.Employees.Add(emp); db.SaveChanges(); } else { db.Entry(emp).State = EntityState.Modified; db.SaveChanges(); } } //return RedirectToAction("ViewAll"); } catch (Exception ex) { } return(RedirectToAction("ViewAll")); }