Exemplo n.º 1
0
        public ActionResult Export()
        {
            List <ContactModel> allContacts = new List <ContactModel>();

            using (MyAddressBookEntities dc = new MyAddressBookEntities())
            {
                var v = (from a in dc.Contacts
                         join b in dc.Countries on a.CountryID equals b.CountryID
                         join c in dc.States on a.StateID equals c.StateID
                         select new ContactModel
                {
                    ContactID = a.ContactID,
                    FirstName = a.ContactPersonFname,
                    LastName = a.ContactPersonLname,
                    ContactNo1 = a.ContactNo1,
                    ContactNo2 = a.ContactNo2,
                    EmailID = a.EmailID,
                    Country = b.CountryName,
                    State = c.StateName,
                    Address = a.Address,
                    ImagePath = a.ImagePath
                }).ToList();
                allContacts = v;
            }
            return(View(allContacts));
        }
Exemplo n.º 2
0
        // Function for get Contact by id, Isolated as we will use this multiple time
        public Contact GetContact(int contactID)
        {
            Contact contact = null;

            using (MyAddressBookEntities dc = new MyAddressBookEntities())
            {
                var v = (from a in dc.Contacts
                         join b in dc.Countries on a.CountryID equals b.CountryID
                         join c in dc.States on a.StateID equals c.StateID
                         where a.ContactID.Equals(contactID)
                         select new
                {
                    a,
                    b.CountryName,
                    c.StateName
                }).FirstOrDefault();
                if (v != null)
                {
                    contact             = v.a;
                    contact.CountryName = v.CountryName;
                    contact.StateName   = v.StateName;
                }
            }
            return(contact);
        }
Exemplo n.º 3
0
        public ActionResult Add()
        {
            //fetch country data
            List <Country> allCountry = new List <Country>();

            //MyAddressBookEntities is the DbContext
            using (MyAddressBookEntities dc = new MyAddressBookEntities())
            {
                allCountry = dc.Countries.OrderBy(a => a.CountryName).ToList();
            }
            ViewBag.Country = new SelectList(allCountry, "CountryID", "CountryName");

            return(View());
        }
Exemplo n.º 4
0
 // Action for fetch state from jquery code
 public JsonResult GetStates(int countryID)
 {
     using (MyAddressBookEntities dc = new MyAddressBookEntities())
     {
         // will off Lazy Loading
         var State = (from a in dc.States
                      where a.CountryID.Equals(countryID)
                      orderby a.StateName
                      select a).ToList();
         return(new JsonResult {
             Data = State, JsonRequestBehavior = JsonRequestBehavior.AllowGet
         });
     }
 }
Exemplo n.º 5
0
 [ActionName("Delete")]  //action name required
 public ActionResult DeleteConfirm(int id)
 {
     using (MyAddressBookEntities dc = new MyAddressBookEntities())
     {
         var address = dc.Addresses.Where(a => a.AddressID.Equals(id)).FirstOrDefault();
         if (address != null)
         {
             dc.Addresses.Remove(address);
             dc.SaveChanges();
             return(RedirectToAction("Index"));
         }
         else
         {
             return(HttpNotFound("Address Not Found!"));
         }
     }
 }
Exemplo n.º 6
0
        // Add
        public ActionResult Add()
        {
            //fetch country data
            List <Country> AllCountry = new List <Country>();
            List <State>   states     = new List <State>();

            // Hre MyAddressBookEntities is our DbContext
            using (MyAddressBookEntities dc = new MyAddressBookEntities())
            {
                AllCountry = dc.Countries.OrderBy(a => a.CountryName).ToList();
                //Not need to fetch state as we dont know witch country user select here
            }

            ViewBag.Country = new SelectList(AllCountry, "CountryID", "CountryName");
            ViewBag.State   = new SelectList(states, "StateID", "StateName");
            return(View());
        }
Exemplo n.º 7
0
 [ActionName("Delete")] // Here Action Name is required as we can not make same signature for Get & Post Method
 public ActionResult DeletrConfirm(int id)
 {
     using (MyAddressBookEntities dc = new MyAddressBookEntities())
     {
         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!"));
         }
     }
 }
Exemplo n.º 8
0
 public List <Address> GetAddressDetails(string search, string sort, string sortDir, int skip, int pageSize, out int totalRecord)
 {
     using (MyAddressBookEntities dc = new MyAddressBookEntities())
     {
         var v = (from a in dc.Addresses
                  where
                  a.Name.Contains(search) ||
                  a.Surname.Contains(search)
                  select a
                  );
         totalRecord = v.Count();
         v           = v.OrderBy(sort + " " + sortDir);
         if (pageSize > 0)
         {
             v = v.Skip(skip).Take(pageSize);
         }
         return(v.ToList());
     }
 }
Exemplo n.º 9
0
        public FileResult ExportData()
        {
            List <ContactModel> allContacts = new List <ContactModel>();

            using (MyAddressBookEntities dc = new MyAddressBookEntities())
            {
                var v = (from a in dc.Contacts
                         join b in dc.Countries on a.CountryID equals b.CountryID
                         join c in dc.States on a.StateID equals c.StateID
                         select new ContactModel
                {
                    ContactID = a.ContactID,
                    FirstName = a.ContactPersonFname,
                    LastName = a.ContactPersonLname,
                    ContactNo1 = a.ContactNo1,
                    ContactNo2 = a.ContactNo2,
                    EmailID = a.EmailID,
                    Country = b.CountryName,
                    State = c.StateName,
                    Address = a.Address,
                    ImagePath = a.ImagePath
                }).ToList();
                allContacts = v;
            }

            var    grid       = new WebGrid(source: allContacts, canPage: false, canSort: false);
            string exportData = grid.GetHtml(
                tableStyle: "table table-responsive",
                columns: grid.Columns(
                    grid.Column("ContactID", "Contact ID"),
                    grid.Column("FirstName", "First Name"),
                    grid.Column("LastName", "Last Name"),
                    grid.Column("ContactNo1", "Contact No1"),
                    grid.Column("ContactNo2", "Contact No2"),
                    grid.Column("EmailID", "Email ID")
                    )
                ).ToHtmlString();

            return(File(new System.Text.UTF8Encoding().GetBytes(exportData),
                        "application/vnd.ms-excel",
                        "Contacts.xls"));
        }
Exemplo n.º 10
0
        // GET: /Home/
        public ActionResult Index(int page = 1, string sort = "Name", string sortDir = "asc", string search = "")
        {
            int pageSize    = 10;
            int totalRecord = 0;

            if (page < 1)
            {
                page = 1;
            }
            int skip = (page * pageSize) - pageSize;
            var data = GetAddressDetails(search, sort, sortDir, skip, pageSize, out totalRecord);

            ViewBag.TotalRows = totalRecord;
            ViewBag.search    = search;
            List <AddressModel> addBooks = new List <AddressModel>();

            //MyAddressBookEntities is datacontext
            using (MyAddressBookEntities dc = new MyAddressBookEntities())
            {
                var v = (from a in dc.Addresses
                         join b in dc.Countries on a.CountryID equals b.CountryID
                         select new AddressModel
                {
                    AddressID = a.AddressID,
                    Name = a.Name,
                    Surname = a.Surname,
                    Address1 = a.Address1,
                    Address2 = a.Address2,
                    Postcode = a.Postcode,
                    Town = a.Town,
                    CountryID = a.CountryID,
                    Email = a.Email,
                    MobileNumber = a.MobileNumber,
                    ImagePath = a.ImagePath
                }).ToList();
                addBooks = v;
            }


            return(View(addBooks));
        }
Exemplo n.º 11
0
        public ActionResult Edit(int id)
        {
            //fetch Address
            Address add = null;

            add = GetAddress(id);
            if (add == null)
            {
                return(HttpNotFound("Address Not Found"));
            }

            //fetch Country
            List <Country> allCountry = new List <Country>();

            using (MyAddressBookEntities dc = new MyAddressBookEntities())
            {
                allCountry = dc.Countries.OrderBy(a => a.CountryName).ToList();
            }
            ViewBag.Country = new SelectList(allCountry, "CountryID", "CountryName", add.CountryID);
            return(View(add));
        }
Exemplo n.º 12
0
        //function for fetch add book details from database by id
        private Address GetAddress(int id)
        {
            Address address = null;

            using (MyAddressBookEntities dc = new MyAddressBookEntities())
            {
                var v = (from a in dc.Addresses
                         join b in dc.Countries on a.CountryID equals b.CountryID
                         where a.AddressID.Equals(id)
                         select new
                {
                    a,
                    b.CountryName
                }).FirstOrDefault();
                if (v != null)
                {
                    address             = v.a;
                    address.CountryName = v.CountryName;
                }
            }
            return(address);
        }
Exemplo n.º 13
0
        // Edit
        public ActionResult Edit(int id)
        {
            // Fetch Contact
            Contact c = null;

            c = GetContact(id); // GetContact I have created in the previous part

            if (c == null)
            {
                return(HttpNotFound("Contact Not Found!"));
            }
            // Fetch Country & State
            List <Country> allCountry = new List <Country>();
            List <State>   states     = new List <State>();

            using (MyAddressBookEntities dc = new MyAddressBookEntities())
            {
                allCountry = dc.Countries.OrderBy(a => a.CountryName).ToList();
                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);
            return(View(c));
        }
Exemplo n.º 14
0
        public ActionResult Edit(Address add, HttpPostedFileBase file)
        {
            //fecth Country for dropdownlist
            #region
            List <Country> allCountry = new List <Country>();
            using (MyAddressBookEntities dc = new MyAddressBookEntities())
            {
                allCountry = dc.Countries.OrderBy(a => a.CountryName).ToList();
            }
            ViewBag.Country = new SelectList(allCountry, "CountryID", "CountryName", add.CountryID);
            #endregion
            //validate if file is selected
            #region
            if (file != null)
            {
                if (file.ContentLength > (512 * 100)) //File size 512KB
                {
                    ModelState.AddModelError("FileErrorMessage", "Size must 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
            //update the Address
            #region
            if (ModelState.IsValid)
            {
                //update
                if (file != null)
                {
                    string savePath = Server.MapPath("~/image");
                    string filename = Guid.NewGuid() + Path.GetExtension(file.FileName);
                    file.SaveAs(Path.Combine(savePath, filename));
                    add.ImagePath = filename;
                }

                using (MyAddressBookEntities dc = new MyAddressBookEntities())
                {
                    var v = dc.Addresses.Where(a => a.AddressID.Equals(add.AddressID)).FirstOrDefault();
                    if (v != null)
                    {
                        v.Name         = add.Name;
                        v.Surname      = add.Surname;
                        v.Address1     = add.Address1;
                        v.Address2     = add.Address2;
                        v.Postcode     = add.Postcode;
                        v.Town         = add.Town;
                        v.CountryID    = add.CountryID;
                        v.Email        = add.Email;
                        v.MobileNumber = add.MobileNumber;
                        if (file != null)
                        {
                            v.ImagePath = add.ImagePath;
                        }
                    }
                    dc.SaveChanges();
                }
                return(RedirectToAction("Index"));
            }
            else
            {
                return(View(add));
            }
            #endregion
        }
Exemplo n.º 15
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 (MyAddressBookEntities dc = new MyAddressBookEntities())
            {
                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)
            {
                //Update Contact
                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 (MyAddressBookEntities dc = new MyAddressBookEntities())
                {
                    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));
            }
        }
Exemplo n.º 16
0
        public ActionResult Add(Address add, HttpPostedFileBase file)
        {
            //fetch Country
            #region
            List <Country> allCountry = new List <Country>();
            using (MyAddressBookEntities dc = new MyAddressBookEntities())
            {
                allCountry = dc.Countries.OrderBy(a => a.CountryName).ToList();
            }
            ViewBag.Country = new SelectList(allCountry, "CountryID", "CountryName", add.CountryID);
            #endregion

            //validate file if selected
            #region
            if (file != null)
            {
                if (file.ContentLength > (512 * 100)) //File size 512KB
                {
                    ModelState.AddModelError("FileErrorMessage", "Size must 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

            //validate model & save to database
            #region
            if (ModelState.IsValid)
            {
                //save
                if (file != null)  //add image
                {
                    string savePath = Server.MapPath("../Image/");
                    string fileName = Guid.NewGuid() + Path.GetExtension(file.FileName);
                    file.SaveAs(Path.Combine(savePath, fileName));
                    add.ImagePath = fileName;
                }

                using (MyAddressBookEntities dc = new MyAddressBookEntities())
                {
                    dc.Addresses.Add(add);
                    try
                    {
                        dc.SaveChanges();
                    }
                    catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
                    {
                        Exception raise = dbEx;
                        foreach (var validationErrors in dbEx.EntityValidationErrors)
                        {
                            foreach (var validationError in validationErrors.ValidationErrors)
                            {
                                string message = string.Format("{0}:{1}",
                                                               validationErrors.Entry.Entity.ToString(),
                                                               validationError.ErrorMessage);
                                // raise a new exception nesting
                                // the current instance as InnerException
                                raise = new InvalidOperationException(message, raise);
                            }
                        }
                        throw raise;
                    }
                }
                return(RedirectToAction("Index"));
            }
            else
            {
                return(View(add));
            }
            #endregion
        }
Exemplo n.º 17
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 (MyAddressBookEntities dc = new MyAddressBookEntities())
     {
         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// Validation file if selected
     if (file != null)
     {
         if (file.ContentLength > (512 * 100))    //512KB
         {
             ModelState.AddModelError("FileErrorMessage", "File size must be withing 512KB");
         }
         string[] allowedType      = new string[] { "image/png", "image/gif", "image/jpeg", "image/jpg" };
         bool     isFlileTypeValid = false;
         foreach (var i in allowedType)
         {
             if (file.ContentType == i.ToString())
             {
                 isFlileTypeValid = true;
                 break;
             }
         }
         if (!isFlileTypeValid)
         {
             ModelState.AddModelError("FileErroeMessage", "Only .png, .gif, .jpg file type allowed");
         }
     }
     #endregion
     #region // Validate Model and Save to DB
     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 (MyAddressBookEntities dc = new MyAddressBookEntities())
         {
             dc.Contacts.Add(c);
             dc.SaveChanges();
         }
         return(RedirectToAction("Index"));
     }
     else
     {
         return(View(c));
     }
     #endregion
 }