public ActionResult EditFlag(int?id, FlagBindingModel model)
        {
            if (this.ModelState.IsValid)
            {
                var country = this.db.Countries.Find(id);
                if (country == null)
                {
                    return(this.HttpNotFound());
                }
                var hasUrl = !string.IsNullOrEmpty(model.Url);
                if (country.Flag != null && hasUrl)
                {
                    this.db.Entry(country.Flag).State = EntityState.Deleted;
                    this.db.SaveChanges();
                }

                if (hasUrl)
                {
                    model.Url = Constants.FlagsFolderPath + model.Url;
                    var flag = Mapper.Map <FlagBindingModel, Flag>(model);
                    country.Flag = flag;

                    this.db.SaveChanges();
                }

                this.ViewBag.CountryName = country.Name;
                return(this.RedirectToAction("Index"));
            }
            return(this.View(model));
        }
        public ActionResult EditFlagLocal(int?id, FlagBindingModel model)
        {
            if (this.ModelState.IsValid)
            {
                var country = this.db.Countries.Find(id);
                if (country == null)
                {
                    return(this.HttpNotFound());
                }

                var hasUrl = !string.IsNullOrEmpty(model.Url);
                if (country.Flag != null && hasUrl)
                {
                    this.db.Entry(country.Flag).State = EntityState.Deleted;
                    this.db.SaveChanges();
                }

                var photo = this.Request.Files["Url"];
                if (photo == null)
                {
                    return(this.View());
                }

                if (hasUrl)
                {
                    var directory = $"{this.Server.MapPath("~")}{Constants.FlagsMapPath}";
                    photo.SaveAs(Path.Combine(directory, photo.FileName));
                    model.Url = Constants.FlagsFolderPath + photo.FileName;
                    var flag = Mapper.Map <FlagBindingModel, Flag>(model);
                    country.Flag = flag;
                    this.db.SaveChanges();
                }

                this.ViewBag.CountryName = country.Name;

                return(this.RedirectToAction("Index"));
            }
            return(this.View());
        }