public ActionResult Create(Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.Add(category);

                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.CompanyId =
                new SelectList(
                    ComBoxHelpers.
                    GetCompanies(),
                    "CompanyId",
                    "NameCompany",
                    category.CompanyId);

            return(View(category));
        }
        public ActionResult Create(State state)
        {
            if (ModelState.IsValid)
            {
                db.States.Add(state);

                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(state));
        }
        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));
        }