Beispiel #1
0
        public ActionResult Create(ConsultancyModel consultancy)
        {
            if (ModelState.IsValid)
            {
                consultancy.AddedDate = DateTime.Now;
                consultancy.ModifiedDate = null;
                consultancy.DeleteFlag = false;
                consultancy.DeletedDate = DateTime.Now;

                string extension = System.IO.Path.GetDirectoryName("~/Areas/Admin/Images/");
                string targetFolder = HttpContext.Server.MapPath("~/Areas/Admin/Images/");
                string targetPath = Path.Combine(targetFolder, Request.Files[0].FileName);

                if (Request.Files[0].ContentLength > 0)
                {

                    //string path1 = Server.MapPath("~/Admin/Images/" + Request.Files[0].FileName);
                    //string path = HttpContext.Server.MapPath("~/Admin/Images/");
                    //string filename = Path.GetFileName(Request.Files[0].FileName);

                    Console.Write(extension);
                    Console.Write(targetPath);
                    Console.Write(targetFolder);

                    Request.Files[0].SaveAs(targetPath);

                    ViewData["Success"] = "Successfully inserted into your data";
                }
                else
                {
                    ViewData["Success"] = "Upload Failed.";
                }
                //return View();

                consultancy.Logo = Request.Files[0].FileName;

                //_dbContext.tbl_consultancy.Add(consultancy); // This function directly called from the database located from the "context" folder
                _consultancyRepository.Insert(consultancy); // This function called from the "Repository" folder which is dependency injection of Unity in App_Start which is also registered in Global.asax
                //_dbContext.SaveChanges(); // This function can only use if direct using through database access located from "context" folder
                return RedirectToAction("/Consultancy");
            }
            ModelState.AddModelError("", "Problem occured for Adding Consultancy Info");
            return View(consultancy);
        }
Beispiel #2
0
        public ActionResult Edit(int id, ConsultancyModel _EditConsultancyModel)
        {
            if(ModelState.IsValid)
            {
                _EditConsultancyModel.AddedDate = DateTime.Now;
                _EditConsultancyModel.ModifiedDate = null;
                _EditConsultancyModel.DeleteFlag = false;
                _EditConsultancyModel.DeletedDate = DateTime.Now;

                //UpdateModel(_EditConsultancyModel);

                //_dbContext.Entry(_EditConsultancyModel).State = EntityState.Modified;
                //_dbContext.SaveChanges();

                _EditConsultancyModel = _dbContext.tbl_consultancy.Find(id);

                if (System.IO.File.Exists(Server.MapPath("~/Areas/Admin/Images/") + _EditConsultancyModel.Logo))
                {
                    System.IO.File.Delete(Server.MapPath("~/Areas/Admin/Images/") + _EditConsultancyModel.Logo);

                    string targetFolder = HttpContext.Server.MapPath("~/Areas/Admin/Images/");
                    string targetPath = Path.Combine(targetFolder, Request.Files[0].FileName);

                    Request.Files[0].SaveAs(targetPath);
                }

                _EditConsultancyModel.Logo = Request.Files[0].FileName;

                _consultancyRepository.Update(_EditConsultancyModel); // This function called from the "Repository" folder which is dependency injection of Unity in App_Start which is also registered in Global.asax

                //_dbContext.tbl_consultancy.Add(_EditConsultancyModel);
                // _dbContext.tbl_consultancy.Attach(_EditConsultancyModel);
                //try
                //{
                //    _dbContext.tbl_consultancy.Attach(_EditConsultancyModel);
                //    var entry = _dbContext.Entry(_EditConsultancyModel);
                //    entry.Property(e => e.Address).IsModified = true;
                //    entry.Property(e => e.City).IsModified = true;
                //    entry.Property(e => e.Description).IsModified = true;
                //    entry.Property(e => e.Name).IsModified = true;
                //    entry.Property(e => e.State).IsModified = true;
                //    entry.Property(e => e.Status).IsModified = true;
                //    entry.Property(e => e.Latitude).IsModified = true;
                //    entry.Property(e => e.Longitude).IsModified = true;
                //   _dbContext.SaveChanges();

                //}
                //catch (Exception e)
                //{
                //    Response.Write(e.Message);
                //    Response.End();
                //}
                //var entry = _dbContext.Entry(_EditConsultancyModel);
                //entry.Property(e => e.Email).IsModified = true;
                // other changed properties

                //_dbContext.Entry(_EditConsultancyModel).State = EntityState.Modified;
                //try
                //{
                //    _dbContext.SaveChanges();

                //}
                //catch (Exception e)
                //{
                //    Response.Write(e.Message);
                //    Response.End();
                //}

                return RedirectToAction("Consultancy");
            }
            ModelState.AddModelError("", "Cannot edit, please try again");
            return View(_EditConsultancyModel);
        }
Beispiel #3
0
        public ActionResult Delete(int id, ConsultancyModel _DeleteConsultancyModel)
        {
            if (ModelState.IsValid)
            {
                _DeleteConsultancyModel = _dbContext.tbl_consultancy.Find(id);
                //_dbContext.tbl_consultancy.Remove(_DeleteConsultancyModel);
                //_dbContext.SaveChanges();

                System.IO.File.Delete(Server.MapPath("~/Areas/Admin/Images/") + _DeleteConsultancyModel.Logo);
                _consultancyRepository.Delete(id); // This function called from the "Repository" folder which is dependency injection of Unity in App_Start which is also registered in Global.asax

                return RedirectToAction("/Consultancy");
            }
            ModelState.AddModelError("", "Cannot delete, please try again");
            return View(_DeleteConsultancyModel);
        }