Ejemplo n.º 1
0
        public ActionResult Input(Country model)
        {
            try
            {
                //TODO: Kiểm tra tính hợp lleej của dự liệu được nhập
                if (string.IsNullOrEmpty(model.CountryName))
                {
                    ModelState.AddModelError("CountryName", "CountryName expected");
                }

                if (!ModelState.IsValid)
                {
                    ViewBag.Title = model.CountryID == 0 ? "Add new Country" : "Edit a Country";
                    return(View(model));
                }
                //TODO: Lưu dữ liệu vào DB
                if (model.CountryID == 0)
                {
                    CatalogBLL.AddCountry(model);
                }
                else
                {
                    CatalogBLL.UpdateCountry(model);
                }
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message + ":" + ex.StackTrace);
                return(View(model));
            }
        }
Ejemplo n.º 2
0
 public ActionResult Input(Country country)
 {
     if (string.IsNullOrEmpty(country.CountryID))
     {
         ModelState.AddModelError("CountryID", "CountryID is not valid");
     }
     if (string.IsNullOrEmpty(country.CountryName))
     {
         ModelState.AddModelError("CountryName", "Country Name is not valid");
     }
     if (ModelState.IsValid)
     {
         Country existCountry = CatalogBLL.GetCountry(country.CountryID);
         if (existCountry == null)
         {
             CatalogBLL.AddCountry(country);
         }
         else
         {
             CatalogBLL.UpdateCountry(country);
         }
     }
     else
     {
         return(View(country));
     }
     return(RedirectToAction("Index"));
 }
Ejemplo n.º 3
0
        public ActionResult Input(Country data)
        {
            if (string.IsNullOrEmpty(data.CountryName))
            {
                ModelState.AddModelError("CountryName", "CountryName Expected");
            }
            if (string.IsNullOrEmpty(data.Code))
            {
                ModelState.AddModelError("Code", "Code Expected");
            }

            if (!ModelState.IsValid)
            {
                return(View(data));
            }
            //TODO: Lưu dữ liệu vào DB
            if (data.CountryID == 0)
            {
                CatalogBLL.AddCountry(data);
            }
            else
            {
                CatalogBLL.UpdateCountry(data);
            }
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 4
0
        public ActionResult Input(Country model)
        {
            //TODO: Kiểm tra tính hợp lệ của dữ liệu được nhập
            if (string.IsNullOrEmpty(model.CountryName))
            {
                ModelState.AddModelError("CountryName", "CountryName Expected");
            }

            if (!ModelState.IsValid)
            {
                ViewBag.Title = model.CountryID == 0 ? "Create new Country" : "Edit Country";
                return(View(model));
            }
            //TODO: Lưu dữ liệu vao DB


            if (model.CountryID == 0)
            {
                CatalogBLL.AddCountry(model);
            }
            else
            {
                CatalogBLL.UpdateCountry(model);
            }
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 5
0
        public IActionResult Input(Country model, string id = "")
        {
            try
            {
                CheckNotNull(model);

                if (string.IsNullOrEmpty(id))
                {
                    CatalogBLL.AddCountry(model);
                }
                else
                {
                    CatalogBLL.UpdateCountry(model);
                }

                return(RedirectToAction("Index"));
            }
            catch (MissingFieldException)
            {
                ViewData["CountryID"]   = model.CountryID;
                ViewData["CountryName"] = model.CountryName;
                return(View(new Country()));
            }
            catch (System.Exception ex)
            {
                _logger.LogError(ex.Message + ": " + ex.StackTrace);
                if (ex.Message.Contains("The duplicate key value is"))
                {
                    ModelState.AddModelError("CountryID", model.CountryID + " is already existed.");
                }

                ViewData["CountryID"]   = model.CountryID;
                ViewData["CountryName"] = model.CountryName;
                return(View(new Country()));
            }
        }
Ejemplo n.º 6
0
        public ActionResult Input(Country model, string Method)
        {   // lam that nho them try catch
            // : Kiểm tra tính hợp lệ của dữ liệu được nhập
            if (string.IsNullOrEmpty(model.CountryName))
            {
                ModelState.AddModelError("CountryName", "*");
            }


            if (Method == "Add" && !string.IsNullOrEmpty(model.CountryName))
            {
                foreach (var x in CatalogBLL.ListCountry())
                {
                    if ((x.CountryName).Contains(Convert.ToString(model.CountryName)))
                    {
                        ModelState.AddModelError("CountryName", "Duplicate Country Name , Please enter another Country Name");
                    }
                }
                ;
            }


            if (Method == "Update")
            {
                foreach (var x in CatalogBLL.ListCountry())
                {
                    if ((x.CountryName).Contains(Convert.ToString(model.CountryName)))
                    {
                        ModelState.AddModelError("CountryName", "Duplicate Country Name , Please enter another Country Name");
                    }
                }
                ;
            }
            if (!ModelState.IsValid)
            {
                if (Method == "Add")
                {
                    ViewBag.Title = "Create new a Country ";
                    SetAlert("Add new Country Fail , Check again!", "danger");
                    //Khi bị lỗi thì hắn đã xóa method nên nó ko phân biệt được
                    ViewBag.Method = "Add";
                }
                else
                {
                    ViewBag.Title = "Edit a Country";
                    SetAlert("Update new Country Fail , Check again!", "danger");
                    ViewBag.Method = "Update";
                }


                return(View(model));
            }

            //: Lưu dữ liệu vào DB
            if (Method == "Add")
            {
                CatalogBLL.AddCountry(model);
                SetAlert("Add new Country success !", "success");
            }
            if (Method == "Update")
            {
                CatalogBLL.UpdateCountry(model);
                SetAlert("Update Country success !", "success");
            }
            return(RedirectToAction("Index"));
        }