Esempio n. 1
0
        public ActionResult Edit(City city)
        {
            if (ModelState.IsValid)
            {
                db.Entry(city).State =
                    EntityState.Modified;

                try
                {
                    db.SaveChanges();

                    return(RedirectToAction("Index"));
                }
                catch (Exception ex)
                {
                    if (ex.InnerException != null &&
                        ex.InnerException.
                        InnerException != null &&
                        ex.InnerException.
                        InnerException.Message.
                        Contains("_Index"))
                    {
                        ModelState.
                        AddModelError(
                            string.Empty,
                            "You Can't Add a New Record, Because There is Already One");
                    }
                    else
                    {
                        ModelState.
                        AddModelError(
                            string.Empty,
                            ex.Message);
                    }
                }
            }

            ViewBag.StateId =
                new SelectList(
                    ComBoxHelpers.
                    GetStates(),
                    "StateId",
                    "NameState",
                    city.StateId);

            return(View(city));
        }
        public ActionResult Edit(Category category)
        {
            if (ModelState.IsValid)
            {
                db.Entry(category).State =
                    EntityState.Modified;

                try
                {
                    db.SaveChanges();

                    return(RedirectToAction("Index"));
                }
                catch (Exception ex)
                {
                    if (ex.InnerException != null &&
                        ex.InnerException.
                        InnerException != null &&
                        ex.InnerException.
                        InnerException.Message.
                        Contains("_Index"))
                    {
                        ModelState.
                        AddModelError(
                            string.Empty,
                            "You Can't Add a New Record, Because There is Already One");
                    }
                    else
                    {
                        ModelState.
                        AddModelError(
                            string.Empty,
                            ex.Message);
                    }
                }
            }

            return(View(category));
        }
        public ActionResult Create(Company company)
        {
            if (ModelState.IsValid)
            {
                db.Companies.Add(company);

                try
                {
                    db.SaveChanges();

                    if (company.LogoFile != null)
                    {
                        var folder = "~/Content/Logos";

                        var file = string.Format("{0}.jpg",
                                                 company.CompanyId);

                        var response =
                            FilesHelpers.
                            UploadPhoto(
                                company.LogoFile,
                                folder, file);

                        if (response)
                        {
                            var pic =
                                string.Format("{0}/{1}",
                                              folder,
                                              file);

                            company.Logo = pic;

                            db.Entry(company).State = EntityState.Modified;

                            db.SaveChanges();
                        }
                    }

                    return(RedirectToAction("Index"));
                }
                catch (Exception ex)
                {
                    if (ex.InnerException != null &&
                        ex.InnerException.
                        InnerException != null &&
                        ex.InnerException.
                        InnerException.Message.
                        Contains("_Index"))
                    {
                        ModelState.
                        AddModelError(
                            string.Empty,
                            "You Can't Add a New Record, Because There is Already One");
                    }
                    else
                    {
                        ModelState.
                        AddModelError(
                            string.Empty,
                            ex.Message);
                    }
                }
            }

            ViewBag.CityId =
                new SelectList(
                    ComBoxHelpers.
                    GetCities(),
                    "CityId",
                    "NameCity",
                    company.CityId);

            ViewBag.StateId =
                new SelectList(
                    ComBoxHelpers.
                    GetStates(),
                    "StateId",
                    "NameState",
                    company.StateId);

            return(View(company));
        }