public ActionResult Create(MyCallTree list) { try { string role = Convert.ToString(Session["RoleName"]); long count = 0; if (db.MyCallTrees.Any()) { count = db.MyCallTrees.Max(m => m.CallTreeID); count++; } else { count = 1000; } var file = Request.Files["file"]; string name = ""; if (file.ContentLength > 0) { string extension = System.IO.Path.GetExtension(file.FileName); name = Convert.ToString(count) + extension; string pic = System.IO.Path.GetFileName(file.FileName); string path = System.IO.Path.Combine(Server.MapPath("~/Uploads/MyCallTree"), name); file.SaveAs(path); list.Images = name; } if (role == "Admin") { if (list.Name == "" || list.Name == null || ModelState.IsValid == false) { int helper = Convert.ToInt32(list.HelperID); ViewBag.HelperID = new SelectList(db.Registrations.Where(m => m.Role != "Admin"), "UserID", "UserName", list.HelperID); ViewBag.UserID = new SelectList(db.UserRegistrations.Where(m => m.HelperID == helper), "UserID", "UserName"); return(View(list)); } } if (ModelState.IsValid) { if (list.HelperID <= 0 || list.HelperID == null) { list.HelperID = Convert.ToInt32(Session["UserID"]); } db.MyCallTrees.Add(list); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(list)); } catch { return(View(list)); } }
// GET: MyCallTree/Delete/5 public ActionResult Delete(int id) { try { MyCallTree lst = db.MyCallTrees.Find(id); db.MyCallTrees.Remove(lst); db.SaveChanges(); return(RedirectToAction("Index")); } catch (Exception ex) { return(View()); } }
public ActionResult Edit(MyCallTree list) { try { int user = Convert.ToInt32(Session["listuser"]); string role = Convert.ToString(Session["RoleName"]); var file = Request.Files["file"]; string name = ""; if (file.ContentLength > 0) { string extension = System.IO.Path.GetExtension(file.FileName); name = Convert.ToString(list.CallTreeID) + extension; string pic = System.IO.Path.GetFileName(file.FileName); string path = System.IO.Path.Combine(Server.MapPath("~/Uploads/MyCallTree"), name); file.SaveAs(path); list.Images = name; } else { list.Images = Convert.ToString(list.Images); } if (role == "Admin") { if (user != list.HelperID) { int helper = Convert.ToInt32(list.HelperID); ViewBag.HelperID = new SelectList(db.Registrations, "UserID", "UserName", list.HelperID); ViewBag.UserID = new SelectList(db.UserRegistrations.Where(m => m.HelperID == helper), "UserID", "UserName"); Session["listuser"] = list.HelperID; return(View(list)); } } if (ModelState.IsValid) { db.Entry(list).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(list)); } catch { return(View(list)); } }