public ActionResult Edit([Bind(Include = "CompanyID,CompanyName,CompanyAddress,CountryID,StateID,CityID,EmailID,WebSite,PhoneNo,CreatedDate,ModifyDate,IsActive,LogoPath")] CompanyModel compnymodel, HttpPostedFileBase file)
        {
            UserPermissionAction("company", RoleAction.edit.ToString());
            CheckPermission();

            try
            {
                TempData["ShowMessage"] = "error";
                TempData["MessageBody"] = "Please fill the required field with valid data";
                if (ModelState.IsValid)
                {
                    Company objcompany = _CompanyService.GetCompanies().Where(c => ((c.CompanyName.ToLower().Trim() == compnymodel.CompanyName.ToLower().Trim()) && c.CompanyID != compnymodel.CompanyID)).FirstOrDefault();


                    //db.tbCompanies.Where(c => c.CompanyName.ToLower().Trim() == tbcompany.CompanyName.ToLower().Trim() && c.CompanyID != tbcompany.CompanyID).FirstOrDefault();
                    if (objcompany == null)
                    {
                        Mapper.CreateMap <CommunicationApp.Models.CompanyModel, CommunicationApp.Entity.Company>();
                        CommunicationApp.Entity.Company compnyentity = Mapper.Map <CommunicationApp.Models.CompanyModel, CommunicationApp.Entity.Company>(compnymodel);
                        _CompanyService.UpdateCompany(compnyentity);
                        // db.Entry(tbcompany).State = EntityState.Modified;

                        var companyList = _CompanyService.GetCompanies().Where(c => c.CompanyID == compnymodel.CompanyID).FirstOrDefault(); //(from m in db.tbCompanies where m.CompanyID == tbcompany.CompanyID select m).FirstOrDefault();
                        var shortPath   = companyList.LogoPath;
                        if (file != null)
                        {
                            if (companyList.LogoPath != "")
                            {   //Delete Old Image
                                string   pathDel = Server.MapPath(companyList.LogoPath);
                                FileInfo objfile = new FileInfo(pathDel);
                                if (objfile.Exists) //check file exsit or not
                                {
                                    objfile.Delete();
                                }
                                //End :Delete Old Image
                            }

                            var fileExt  = Path.GetExtension(file.FileName);
                            var fileName = compnymodel.CompanyID.ToString() + fileExt;
                            var subPath  = Server.MapPath("~/CompanyLogo");
                            //Check SubPath Exist or Not
                            if (!Directory.Exists(subPath))
                            {
                                Directory.CreateDirectory(subPath);
                            }
                            //End : Check SubPath Exist or Not

                            var path = Path.Combine(subPath, fileName);
                            shortPath = "~/CompanyLogo/" + fileName;
                            file.SaveAs(path);
                            CommonCls.CreateThumbnail(shortPath, 218, 84, false);

                            //Update the Path in Database
                            Company compnyupdate = _CompanyService.GetCompanies().Where(c => c.CompanyID == compnymodel.CompanyID).OrderByDescending(c => c.CompanyID).FirstOrDefault();//(from m in db.tbCompanies where m.CompanyID == tbcompany.CompanyID orderby m.CompanyID descending select m).FirstOrDefault();
                            compnyupdate.LogoPath = shortPath;
                            _CompanyService.UpdateCompany(compnyupdate);

                            //End : Update the Path in Database
                        }


                        TempData["ShowMessage"] = "success";
                        TempData["MessageBody"] = compnymodel.CompanyName + " is update successfully.";
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        TempData["ShowMessage"] = "error";
                        TempData["MessageBody"] = compnymodel.CompanyName + " is already exists.";
                    }
                }
            }

            catch (RetryLimitExceededException)
            {
                TempData["ShowMessage"] = "error";
                TempData["MessageBody"] = "Some unknown problem occured while proccessing save operation on " + compnymodel.CompanyName + " ";
            }
            ViewBag.CityID      = (compnymodel.CityID <= 0 ? "" : compnymodel.CityID.ToString());
            ViewBag.StateID     = (compnymodel.StateID <= 0 ? "" : compnymodel.StateID.ToString());
            ViewBag.Countrylist = new SelectList(_CountryService.GetCountries(), "CountryID", "CountryName", compnymodel.CountryID);
            ViewBag.Citylist    = new SelectList(_CityService.GetCities(), "CityID", "CityName", compnymodel.CityID);

            ViewBag.Statelist = new SelectList(_StateService.GetStates(), "StateID", "StateName", compnymodel.StateID);
            //SetFieldsForEdit(compnymodel);
            return(View(compnymodel));
        }
        public ActionResult Create([Bind(Include = "CompanyName,CompanyAddress,CountryID,StateID,CityID,EmailID,WebSite,PhoneNo,CreatedDate,ModifyDate,IsActive,LogoPath")] CompanyModel compnymodel, HttpPostedFileBase file)
        {
            UserPermissionAction("company", RoleAction.create.ToString());
            CheckPermission();
            try
            {
                TempData["ShowMessage"] = "error";
                TempData["MessageBody"] = "Please fill the required field with valid data";
                if (ModelState.IsValid)
                {
                    Company objcompany = _CompanyService.GetCompanies().Where(c => c.CompanyName.ToLower() == compnymodel.CompanyName).FirstOrDefault();

                    if (objcompany == null)
                    {
                        Mapper.CreateMap <CommunicationApp.Models.CompanyModel, CommunicationApp.Entity.Company>();
                        CommunicationApp.Entity.Company compnyentity = Mapper.Map <CommunicationApp.Models.CompanyModel, CommunicationApp.Entity.Company>(compnymodel);
                        //objcompany.LogoPath = ""; //this will reset after logo upload to local folder.


                        //Save the Logo in Folder
                        int CompanyID = compnymodel.CompanyID;
                        var fileExt   = Path.GetExtension(file.FileName);
                        var fileName  = CompanyID.ToString() + fileExt;
                        var subPath   = Server.MapPath("~/CompanyLogo");
                        //Check SubPath Exist or Not
                        if (!Directory.Exists(subPath))
                        {
                            Directory.CreateDirectory(subPath);
                        }
                        //End : Check SubPath Exist or Not

                        var path = Path.Combine(subPath, fileName);
                        file.SaveAs(path);

                        var shortPath = "~/CompanyLogo/" + fileName;
                        CommonCls.CreateThumbnail(shortPath, 218, 84, false);
                        compnyentity.LogoPath = shortPath;
                        _CompanyService.InsertCompany(compnyentity);

                        //Update the Path in Database
                        //Company companyUpdate = _CompanyService.GetCompanies().Where(c => c.CompanyID == CompanyID).OrderBy(c => c.CompanyID).FirstOrDefault(); //(from m in db.tbCompanies where m.CompanyID == CompanyID orderby m.CompanyID descending select m).FirstOrDefault();
                        //companyUpdate.LogoPath = shortPath;
                        //_CompanyService.UpdateCompany(companyUpdate);
                        //// db.SaveChanges();
                        ////End : Update the Path in Database

                        TempData["ShowMessage"] = "success";
                        TempData["MessageBody"] = compnymodel.CompanyName + " is saved successfully.";
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        TempData["ShowMessage"] = "error";
                        TempData["MessageBody"] = compnymodel.CompanyName + " is already exists.";
                    }
                }
            }

            catch (RetryLimitExceededException)
            {
                TempData["ShowMessage"] = "error";
                TempData["MessageBody"] = "Some unknown problem occured while proccessing save operation on " + compnymodel.CompanyName + " Company";
            }

            //ViewBag.CountryID = new SelectList(db.tbCountries, "CountryID", "CountryName");
            ViewBag.CountryID = new SelectList(_CountryService.GetCountries(), "CountryID", "CountryName", compnymodel.CountryID);

            return(RedirectToAction("Create", "Company", new { id = compnymodel.CompanyID }));
        }