コード例 #1
0
 public ActionResult Create(MyNeighbor list)
 {
     try
     {
         long count = 0;
         if (db.MyNeighbors.Any())
         {
             count = db.MyNeighbors.Max(m => m.NeighborID);
             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/MyNeighbors"), name);
             file.SaveAs(path);
             list.Images = name;
         }
         string role = Convert.ToString(Session["RoleName"]);
         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());
             }
         }
         if (ModelState.IsValid)
         {
             if (list.HelperID <= 0 || list.HelperID == null)
             {
                 list.HelperID = Convert.ToInt32(Session["UserID"]);
             }
             db.MyNeighbors.Add(list);
             db.SaveChanges();
             return(RedirectToAction("Index"));
         }
         return(View(list));
     }
     catch
     {
         return(View(list));
     }
 }
コード例 #2
0
 // GET: MyNeighbors/Delete/5
 public ActionResult Delete(int id)
 {
     try
     {
         MyNeighbor lst = db.MyNeighbors.Find(id);
         db.MyNeighbors.Remove(lst);
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     catch (Exception ex)
     {
         return(View());
     }
 }
コード例 #3
0
 public ActionResult Edit(MyNeighbor 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.NeighborID) + extension;
             string pic  = System.IO.Path.GetFileName(file.FileName);
             string path = System.IO.Path.Combine(Server.MapPath("~/Uploads/MyNeighbors"), 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));
     }
 }