public IActionResult Create(PoliticalPartyLanguagesCreateViewModel pvn)
        {
            if (pvn is null)
            {
                throw new ArgumentNullException(nameof(pvn));
            }

            string uniqueFileName = null;
            string originalpath   = null;

            if (pvn.EnglishPartyID == -2)
            {
                if (pvn.PartyPhoto != null)
                {
                    UploadPhoto(pvn, out uniqueFileName, out originalpath);
                }
            }

            try
            {
                int i = db.Database.ExecuteSqlInterpolated($"EXEC [PoliticalPartyInsert] @LanguageID = {pvn.LanguageID}, @Name={pvn.Name},@StateID = {pvn.StateID}, @EnglishPartyID = {pvn.EnglishPartyID} , @PhotoPath = {originalpath}");

                if (i > 0)
                {
                    return(RedirectToAction("index"));
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.InnerException.Message);
            }

            return(View(pvn));
        }
        public IActionResult Edit(int plangid)
        {
            GetDropDOwnList();
            var pol = db.PoliticalPartyLanguagesEditViewModel.FromSqlRaw($"EXEC PoliticalPartyByID @partyid = {plangid}").ToList().FirstOrDefault();
            PoliticalPartyLanguagesCreateViewModel pcm = new PoliticalPartyLanguagesCreateViewModel();

            pcm.EnglishPartyID           = pol.EnglishPartyID;
            pcm.PoliticalPartyLanguageId = pol.PoliticalPartyLanguageId;
            pcm.StateID        = pol.StateID;
            pcm.Name           = pol.Name;
            pcm.LanguageId     = pol.LanguageId;
            pcm.EnglishPartyID = pol.EnglishPartyID;


            return(View(pcm));
        }
        public IActionResult Edit(PoliticalPartyLanguagesCreateViewModel pl)
        {
            if (pl is null)
            {
                throw new ArgumentNullException(nameof(pl));
            }

            string uniqueFileName = null;
            string originalpath   = null;

            if (pl.EnglishPartyID == -2)
            {
                if (pl.PartyPhoto != null)
                {
                    UploadPhoto(pl, out uniqueFileName, out originalpath);
                }
            }
            PoliticalPartyLanguages p = new PoliticalPartyLanguages();

            if (string.IsNullOrEmpty(originalpath))
            {
                // var pol = plangrepo.GetByID(pl.PoliticalPartyLanguageId);
                p.PartyPhotoPath = pl.OldPhotoPath;
            }
            else
            {
                p.PartyPhotoPath = originalpath;
            }

            p.Name    = pl.Name;
            p.StateID = pl.StateID;
            p.PoliticalPartyLanguageId = pl.PoliticalPartyLanguageId;
            p.LanguageId     = pl.LanguageId;
            p.EnglishPartyID = pl.EnglishPartyID;

            int i = plangrepo.Update(p);

            if (i > 0)
            {
                return(RedirectToAction("index"));
            }
            return(View(pl));
        }
        private void UploadPhoto(PoliticalPartyLanguagesCreateViewModel pvn, out string uniqueFileName, out string originalpath)
        {
            string partypath = null;

            partypath      = Path.Combine(hostingEnvironment.WebRootPath, "partyimages");
            uniqueFileName = Path.GetFileNameWithoutExtension(pvn.PartyPhoto.FileName) + "_" + Guid.NewGuid().ToString() + Path.GetExtension(pvn.PartyPhoto.FileName);
            string filepath = Path.Combine(partypath, uniqueFileName);

            originalpath = $"/partyimages/{uniqueFileName}";
            try
            {
                using (FileStream fs = new FileStream(filepath, FileMode.Create))
                {
                    pvn.PartyPhoto.CopyTo(fs);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.InnerException.Message);
            }
        }