Ejemplo n.º 1
0
        public IActionResult Input(Customer model)
        {
            try
            {
                CheckNotNull(model);
                SetEmptyNullableField(model);

                // Save data into DB
                if (model.CustomerID == null)
                {
                    model.CustomerID = Guid.NewGuid().ToString().Substring(0, 5);;
                    CatalogBLL.AddCustomer(model);
                }
                else
                {
                    CatalogBLL.UpdateCustomer(model);
                }
                return(RedirectToAction("Index"));
            }
            catch (MissingFieldException)
            {
                return(View(model));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message + ":\n" + ex.StackTrace);
                return(View(model));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// tao moi hoac sua
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult Input(Customer model)
        {
            try
            {
                // kiểm tra dữ liệu vào
                if (string.IsNullOrEmpty(model.CustomerID) || model.CustomerID.Length != 5)
                {
                    ModelState.AddModelError("CustomerID", "CustomerID is invalid");
                }
                if (string.IsNullOrEmpty(model.Country))
                {
                    model.Country = "";
                }
                if (string.IsNullOrEmpty(model.ContactTitle))
                {
                    model.ContactTitle = "";
                }
                if (string.IsNullOrEmpty(model.ContactName))
                {
                    model.ContactName = "";
                }
                if (string.IsNullOrEmpty(model.CompanyName))
                {
                    model.CompanyName = "";
                }
                if (string.IsNullOrEmpty(model.City))
                {
                    model.City = "";
                }
                if (string.IsNullOrEmpty(model.Address))
                {
                    model.Address = "";
                }

                if (ModelState.IsValid)
                {
                    // Lưu dữ liệu vào DB
                    Customer existCustomer = CatalogBLL.GetCustomer(model.CustomerID);
                    if (existCustomer == null)
                    {
                        CatalogBLL.AddCustomer(model);
                    }
                    else
                    {
                        CatalogBLL.UpdateCustomer(model);
                    }
                    return(RedirectToAction("Index"));
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message + ": " + ex.StackTrace);
            }
            return(View(model));
        }
Ejemplo n.º 3
0
        public ActionResult Input(Customer model, string Method)
        {
            //TODO: Kiểm tra tính hợp lệ của dữ liệu được nhập
            if (string.IsNullOrEmpty(model.CustomerID) || model.CustomerID.Length != 5)
            {
                ModelState.AddModelError("CustomerID", "CompanyName Expected");
            }
            if (string.IsNullOrEmpty(model.CompanyName))
            {
                ModelState.AddModelError("CompanyName", "CompanyName Expected");
            }
            if (string.IsNullOrEmpty(model.ContactName))
            {
                ModelState.AddModelError("ContactName", "ContactName Expected");
            }
            if (string.IsNullOrEmpty(model.ContactTitle))
            {
                ModelState.AddModelError("ContactTitle", "ContactTitle Expected");
            }

            if (string.IsNullOrEmpty(model.Address))
            {
                model.Address = "";
            }
            if (string.IsNullOrEmpty(model.Country))
            {
                model.Country = "";
            }
            if (string.IsNullOrEmpty(model.City))
            {
                model.City = "";
            }
            if (string.IsNullOrEmpty(model.Address))
            {
                model.Address = "";
            }
            if (string.IsNullOrEmpty(model.Phone))
            {
                model.Phone = "";
            }
            if (string.IsNullOrEmpty(model.Fax))
            {
                model.Fax = "";
            }

            if (!ModelState.IsValid)
            {
                // ViewBag.Title = model.CustomerID == "" ? "Creat new customer" : "Edit a customer";

                // Method = ViewBag.Method == "Add" ? "Add" : "Edit";
                ViewBag.Title  = Method == "Add" ? "Creat new customer" : "Edit a customer";
                ViewBag.Method = Method;
                return(View(model));
            }
            //TODO: Lưu dữ liệu vao DB

            Customer exitsCustomer = CatalogBLL.GetCustomer(model.CustomerID);

            if (Method == "Add")
            {
                if (exitsCustomer != null)
                {
                    ViewBag.Title  = Method == "Add" ? "Creat new customer" : "Edit a customer";
                    ViewBag.Method = Method;
                    ModelState.AddModelError("CustomerID", "CustomerID exits");
                    return(View(model));
                }
                CatalogBLL.AddCustomer(model);
            }
            else
            {
                CatalogBLL.UpdateCustomer(model);
            }
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 4
0
        public ActionResult Input(Customer data, string method)
        {
            //try
            //{
            //TODO: kiểm tr tính hợp lệ của dữ liệu
            if (string.IsNullOrEmpty(data.CompanyName))
            {
                ModelState.AddModelError("CompanyName", "CompanyName Expected");
            }
            if (string.IsNullOrEmpty(data.ContactName))
            {
                ModelState.AddModelError("ContactName", "ContactName Expected");
            }
            if (string.IsNullOrEmpty(data.ContactTitle))
            {
                ModelState.AddModelError("ContactTitle", "ContactTitle Expected");
            }

            if (string.IsNullOrEmpty(data.Address))
            {
                data.Address = "";
            }
            if (string.IsNullOrEmpty(data.Country))
            {
                data.Country = "";
            }
            if (string.IsNullOrEmpty(data.City))
            {
                data.City = "";
            }
            if (string.IsNullOrEmpty(data.Phone))
            {
                data.Phone = "";
            }
            if (string.IsNullOrEmpty(data.Fax))
            {
                data.Fax = "";
            }
            if (string.IsNullOrEmpty(data.Address))
            {
                data.Address = "";
            }
            if (!ModelState.IsValid)
            {
                return(View(data));
            }
            //TODO: Lưu dữ liệu vào DB
            if (method == "Add")
            {
                CatalogBLL.AddCustomer(data);
            }
            else
            {
                CatalogBLL.UpdateCustomer(data);
            }
            return(RedirectToAction("Index"));
            //}
            //catch (Exception ex)
            //{
            //    ModelState.AddModelError("", ex.Message + ": " + ex.StackTrace);
            //    return View(data);
            //}
        }
        public ActionResult Input(Customer model, int kt)
        {
            try
            {
                //TODO :Kiểm tra tính hợp lệ của dữ liệu nhập vào
                if (string.IsNullOrEmpty(model.CustomerID))
                {
                    ModelState.AddModelError("CustomerID", "CompanyName expected");
                }
                if (string.IsNullOrEmpty(model.CompanyName))
                {
                    ModelState.AddModelError("CompanyName", "CompanyName expected");
                }
                if (string.IsNullOrEmpty(model.ContactName))
                {
                    ModelState.AddModelError("ContactName", "ContactName expected");
                }
                if (string.IsNullOrEmpty(model.ContactTitle))
                {
                    ModelState.AddModelError("ContactTitle", "ContactTitle expected");
                }
                if (string.IsNullOrEmpty(model.Address))
                {
                    model.Address = "";
                }
                if (string.IsNullOrEmpty(model.Country))
                {
                    model.Country = "";
                }
                if (string.IsNullOrEmpty(model.City))
                {
                    model.City = "";
                }
                if (string.IsNullOrEmpty(model.Phone))
                {
                    model.Phone = "";
                }
                if (string.IsNullOrEmpty(model.Fax))
                {
                    model.Fax = "";
                }

                //TODO :Lưu dữ liệu nhập vào
                if ((Convert.ToInt32(kt)) == 0)
                {
                    ViewBag.kt = 0;
                    CatalogBLL.AddCustomer(model);
                }
                else if ((Convert.ToInt32(kt)) == 1)
                {
                    ViewBag.kt = 1;
                    CatalogBLL.UpdateCustomer(model);
                }
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message + ":" + ex.StackTrace);
                return(View(model));
            }
        }
Ejemplo n.º 6
0
        public ActionResult Input(Customer model)
        {
            try
            {
                //TODO: Kiểm tra tính hợp lleej của dự liệu được nhập
                if (string.IsNullOrEmpty(model.CompanyName))
                {
                    ModelState.AddModelError("CompanyName", "CompanyName expected");
                }
                if (string.IsNullOrEmpty(model.ContactName))
                {
                    ModelState.AddModelError("ContactName", "ContactName expected");
                }
                if (string.IsNullOrEmpty(model.ContactTitle))
                {
                    ModelState.AddModelError("ContactTitle", "ContactTitle expected");
                }
                if (string.IsNullOrEmpty(model.Address))
                {
                    ModelState.AddModelError("Address", "Address expected");
                }
                if (string.IsNullOrEmpty(model.City))
                {
                    model.City = "";
                }
                if (string.IsNullOrEmpty(model.Country))
                {
                    model.Country = "";
                }
                if (string.IsNullOrEmpty(model.Phone))
                {
                    model.Phone = "";
                }
                if (string.IsNullOrEmpty(model.Fax))
                {
                    model.Fax = "";
                }

                if (!ModelState.IsValid)
                {
                    ViewBag.Title = model.CustomerID == "" ? "Add new Customer" : "Edit a Customer";
                    return(View(model));
                }
                //TODO: Lưu dữ liệu vào DB
                Customer existCustomer = CatalogBLL.GetCustomer(model.CustomerID);
                if (existCustomer == null)
                {
                    CatalogBLL.AddCustomer(model);
                }
                else
                {
                    CatalogBLL.UpdateCustomer(model);
                }
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message + ":" + ex.StackTrace);
                return(View(model));
            }
        }
Ejemplo n.º 7
0
        public ActionResult Input(Customer model, string Method)
        {   // lam that nho them try cacht
            if (string.IsNullOrEmpty(model.CustomerID))
            {
                ModelState.AddModelError("CustomerID", "*");
            }

            if (string.IsNullOrEmpty(model.CompanyName))
            {
                ModelState.AddModelError("CompanyName", "*");
            }

            if (string.IsNullOrEmpty(model.ContactTitle))
            {
                ModelState.AddModelError("ContactTitle", "*");
            }

            if (string.IsNullOrEmpty(model.ContactName))
            {
                ModelState.AddModelError("ContactName", "*");
            }


            if (string.IsNullOrEmpty(model.Address))
            {
                model.Address = "";
            }
            if (string.IsNullOrEmpty(model.City))
            {
                model.City = "";
            }
            if (string.IsNullOrEmpty(model.Country))
            {
                model.Country = "";
            }
            if (string.IsNullOrEmpty(model.Phone))
            {
                model.Phone = "";
            }
            if (string.IsNullOrEmpty(model.Fax))
            {
                model.Fax = "";
            }

            // Chưa tối ưu phần trùng ID=> fixed , check again
            if (Method == "Add")
            {
                foreach (var x in CatalogBLL.ListOfCustomerID())
                {
                    if ((x.CustomerID).Contains(Convert.ToString(model.CustomerID)))
                    {
                        ModelState.AddModelError("CustomerID", "Duplicate ID , Please enter another ID");
                    }
                }
                ;
            }


            if (!ModelState.IsValid)
            {
                ViewBag.Title = "Create new a Customer ";
                if (Method == "Add")
                {
                    SetAlert("Add new Customer 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
                {
                    SetAlert("Update new Customer Fail , Check again!", "danger");
                    ViewBag.Method = "Update";
                }

                return(View(model));
            }


            //: Add dữ liệu vào bị chuyển sang Update, do
            // =>dung viewbag.method
            if (Method == "Add")
            {
                CatalogBLL.AddCustomer(model);
                SetAlert("Add new Customer success !", "success");
            }
            if (Method == "Update")
            {
                CatalogBLL.UpdateCustomer(model);
                SetAlert("Update Customer success !", "success");
            }

            return(RedirectToAction("Index"));
        }