Пример #1
0
 [ActionName("Delete")] // Here Action Name is required as we can not make same signature for Get & Post Method
 public ActionResult DeleteConfirm(int id)
 {
     using (MyContactBookEntities dc = new MyContactBookEntities())
     {
         var contact = dc.Contacts.Where(a => a.ContactID.Equals(id)).FirstOrDefault();
         if (contact != null)
         {
             dc.Contacts.Remove(contact);
             dc.SaveChanges();
             return(RedirectToAction("Index"));
         }
         else
         {
             return(HttpNotFound("Contact Not Found!"));
         }
     }
 }
Пример #2
0
 public ActionResult Add(Contact c, HttpPostedFileBase file)
 {
     #region // Fetch Country & State
     List <Country> allCountry = new List <Country>();
     List <State>   states     = new List <State>();
     using (MyContactBookEntities dc = new MyContactBookEntities())
     {
         allCountry = dc.Countries.OrderBy(a => a.CountryName).ToList();
         if (c.CountryID > 0)
         {
             states = dc.States.Where(a => a.CountryID.Equals(c.CountryID)).OrderBy(a => a.StateName).ToList();
         }
     }
     ViewBag.Country = new SelectList(allCountry, "CountryID", "CountryName", c.CountryID);
     ViewBag.State   = new SelectList(states, "StateID", "StateName", c.StateID);
     #endregion
     #region// Validate file if selected
     if (file != null)
     {
         if (file.ContentLength > (512 * 1000)) // 512 KB
         {
             ModelState.AddModelError("FileErrorMessage", "File size must be within 512 KB");
         }
         string[] allowedType     = new string[] { "image/png", "image/gif", "image/jpeg", "image/jpg" };
         bool     isFileTypeValid = false;
         foreach (var i in allowedType)
         {
             if (file.ContentType == i.ToString())
             {
                 isFileTypeValid = true;
                 break;
             }
         }
         if (!isFileTypeValid)
         {
             ModelState.AddModelError("FileErrorMessage", "Only .png, .gif and .jpg file type allowed");
         }
     }
     #endregion
     #region// Validate Model & Save to Database
     if (ModelState.IsValid)
     {
         //Save here
         if (file != null)
         {
             string savePath = Server.MapPath("~/Image");
             string fileName = Guid.NewGuid() + Path.GetExtension(file.FileName);
             file.SaveAs(Path.Combine(savePath, fileName));
             c.ImagePath = fileName;
         }
         using (MyContactBookEntities dc = new MyContactBookEntities())
         {
             dc.Contacts.Add(c);
             dc.SaveChanges();
         }
         return(RedirectToAction("Index"));
     }
     else
     {
         return(View(c));
     }
     #endregion
 }
Пример #3
0
        public ActionResult Edit(Contact c, HttpPostedFileBase file)
        {
            #region//fetch country & state for dropdown

            List <Country> allCountry = new List <Country>();
            List <State>   states     = new List <State>();
            using (MyContactBookEntities dc = new MyContactBookEntities())
            {
                allCountry = dc.Countries.OrderBy(a => a.CountryName).ToList();
                if (c.CountryID > 0)
                {
                    states = dc.States.Where(a => a.CountryID.Equals(c.CountryID)).OrderBy(a => a.StateName).ToList();
                }
            }
            ViewBag.Country = new SelectList(allCountry, "CountryID", "CountryName", c.CountryID);
            ViewBag.State   = new SelectList(states, "StateID", "StateName", c.StateID);

            #endregion
            #region//validate file is selected
            if (file != null)
            {
                if (file.ContentLength > (512 * 1000)) // 512 KB
                {
                    ModelState.AddModelError("FileErrorMessage", "File size must be within 512KB");
                }
                string[] allowedType     = new string[] { "image/png", "image/gif", "image/jpg", "image/jpeg" };
                bool     isFileTypeValid = false;
                foreach (var i in allowedType)
                {
                    if (file.ContentType == i.ToString())
                    {
                        isFileTypeValid = true;
                        break;
                    }
                }
                if (!isFileTypeValid)
                {
                    ModelState.AddModelError("FileErrorMessage", "Only .png, .gif and .jpg file allowed");
                }
            }
            #endregion
            // Update Contact
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    string savePath = Server.MapPath("~/image");
                    string fileName = Guid.NewGuid() + Path.GetExtension(file.FileName);
                    file.SaveAs(Path.Combine(savePath, fileName));
                    c.ImagePath = fileName;
                }

                using (MyContactBookEntities dc = new MyContactBookEntities())
                {
                    var v = dc.Contacts.Where(a => a.ContactID.Equals(c.ContactID)).FirstOrDefault();
                    if (v != null)
                    {
                        v.ContactPersonFname = c.ContactPersonFname;
                        v.ContactPersonLname = c.ContactPersonLname;
                        v.ContactNo1         = c.ContactNo1;
                        v.ContactNo2         = c.ContactNo2;
                        v.EmailID            = c.EmailID;
                        v.CountryID          = c.CountryID;
                        v.StateID            = c.StateID;
                        v.Address            = c.Address;
                        if (file != null)
                        {
                            v.ImagePath = c.ImagePath;
                        }
                    }
                    dc.SaveChanges();
                }
                return(RedirectToAction("Index"));
            }
            else
            {
                return(View(c));
            }
        }