Example #1
0
        public ActionResult UserProfile(Employee user, HttpPostedFileBase ImageUrl)
        {
            if (Account.User == null)
            {
                return(RedirectToAction("Login"));
            }
            try
            {
                if (db.GetByNIC(Account.User.NIC) == null)
                {
                    return(RedirectToAction("Login"));
                }

                if (ImageUrl != null)
                {
                    if (System.IO.File.Exists($"~/Content/Images/Users/{user.Picture}"))
                    {
                        System.IO.File.Delete($"~/Content/Images/Users/{user.Picture}");
                    }

                    var    str       = "";
                    string ImageName = Path.GetFileName(ImageUrl.FileName);
                    do
                    {
                        str = GetName();
                    } while (System.IO.File.Exists($"~/Content/Images/Users/{str}{ImageName}"));
                    string physicalPath = Server.MapPath($"~/Content/Images/Users/{str}{ImageName}");
                    ImageUrl.SaveAs(physicalPath);
                    user.Picture = $"{str}{ImageName}";
                }
                db.Update(user);
                Account.User = new Employee();
                Account.User = db.GetByNIC(user.NIC);
                return(RedirectToAction("UserProfile"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }
            return(View(user));
        }
        public ActionResult Edit(Employee employee, HttpPostedFileBase ImageUrl)
        {
            var action = new CheckController().CheckStatus("Employees");

            if (action != null)
            {
                return(action);
            }
            try
            {
                if (ModelState.IsValid)
                {
                    if (ddb.GetUNDepartment().ID == employee.Department_ID)
                    {
                        throw new Exception($"Cannot Update Department's Employee To Empty Or 'Uknown' Department");
                    }

                    if (ImageUrl != null)
                    {
                        if (System.IO.File.Exists($"~/Content/Images/Users/{employee.Picture}"))
                        {
                            System.IO.File.Delete($"~/Content/Images/Users/{employee.Picture}");
                        }

                        var    str       = "";
                        string ImageName = Path.GetFileName(ImageUrl.FileName);
                        do
                        {
                            str = GetName();
                        } while (System.IO.File.Exists($"~/Content/Images/Users/{str}{ImageName}"));
                        string physicalPath = Server.MapPath($"~/Content/Images/Users/{str}{ImageName}");
                        ImageUrl.SaveAs(physicalPath);
                        employee.Picture = $"{str}{ImageName}";
                    }
                    db.Update(employee);
                    return(RedirectToAction("Index"));
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }
            ViewBag.Department_ID = new SelectList(new MDepartments().Get_All()
                                                   .Where(i => i.Title.CompareTo("Unknown") != 0).ToList(), "ID", "Title", employee.Department_ID);
            return(View(employee));
        }