public ActionResult Input(Customer model, string method) { try { if (string.IsNullOrEmpty(model.CompanyName)) { ModelState.AddModelError("CompanyName", "Company Name is required"); } if (string.IsNullOrEmpty(model.Phone)) { ModelState.AddModelError("Phone", "Phone is required"); } if (!ModelState.IsValid) { return(View(model)); } Customer customer = CatalogBLL.Customer_Get(model.CustomerID); if (customer == null) { int customerId = CatalogBLL.Customer_Add(model); return(RedirectToAction("Index")); } else { bool updateResult = CatalogBLL.Customer_Update(model); return(RedirectToAction("Index")); } } catch (Exception ex) { ModelState.AddModelError("", ex.Message + ":" + ex.StackTrace); return(View(model)); } }
public ActionResult Input(Customer model, string method) { try { if (String.Equals(method, "Add") == true) { Customer customer = CatalogBLL.Customer_Get(model.CustomerID); if (customer != null) { ModelState.AddModelError("id", "ID customer đã tồn tại"); } if (String.IsNullOrEmpty(model.CompanyName)) { ModelState.AddModelError("CompanyName", "Company Name is required"); } if (String.IsNullOrEmpty(model.Country)) { ModelState.AddModelError("errorAddr", "Vui lòng chọn quốc gia"); } if (!ModelState.IsValid) { ViewBag.method = method; ViewBag.CustomerID = model.CustomerID; return(View(model)); } else { var addCustomer = CatalogBLL.Customer_Add(model); return(RedirectToAction("Index")); } } //if (String.Equals(method, "Edit") == true) else { bool editCustomer = CatalogBLL.Customer_Update(model); return(RedirectToAction("Index")); } } catch (Exception e) { ModelState.AddModelError("", e.Message + ":" + e.StackTrace); return(View(model)); } }
public ActionResult Input(Customer model, string method, string tempID) { if (string.IsNullOrEmpty(method)) { return(RedirectToAction("Index")); } else { if (method == "add") { if (!string.IsNullOrEmpty(model.CustomerID)) { if (CatalogBLL.Customer_Get(model.CustomerID) != null) { ModelState.AddModelError("CustomerID", "Customer ID ready exist"); } } } } if (string.IsNullOrEmpty(model.Fax)) { model.Fax = ""; } //Nếu không có trường method thì chuyển hướng về Index if (string.IsNullOrEmpty(method)) { return(RedirectToAction("Index")); } else { if (string.IsNullOrEmpty(model.Fax)) { model.Fax = ""; } //Kiểm tra có tồn tại bất kỳ lỗi nào hay không if (!ModelState.IsValid) { if (method == "add") { ViewBag.Title = "Add New Customer"; ViewBag.ConfirmButton = "Add"; ViewBag.Method = "add"; return(View(model)); } else { ViewBag.Title = "Edit Customer"; ViewBag.ConfirmButton = "Save"; ViewBag.Method = "update"; return(View(model)); } } //Đưa dữ liệu vào CSDL if (method == "add") { int customerID = CatalogBLL.Customer_Add(model); } else if (method == "update") { bool rs = CatalogBLL.Customer_Update(model); } return(RedirectToAction("Index")); } }
public ActionResult Input(Customer model) { try { // Check if (string.IsNullOrEmpty(model.CustomerID)) { ModelState.AddModelError("CustomerID", "ID is required"); } if (string.IsNullOrEmpty(model.CompanyName)) { ModelState.AddModelError("CompanyName", "Company Name is required"); } if (string.IsNullOrEmpty(model.ContactName)) { ModelState.AddModelError("ContactName", "Contact Name is required"); } if (string.IsNullOrEmpty(model.ContactTitle)) { ModelState.AddModelError("ContactTitle", "Contact Title is required"); } if (string.IsNullOrEmpty(model.Address)) { model.Address = ""; } if (string.IsNullOrEmpty(model.City)) { model.City = ""; } if (string.IsNullOrEmpty(model.Country) || model.Country == "NoSelect") { ModelState.AddModelError("Country", "Country is required"); } if (string.IsNullOrEmpty(model.Phone)) { ModelState.AddModelError("Phone", "Phone is required"); } if (string.IsNullOrEmpty(model.Fax)) { model.Fax = ""; } if (!ModelState.IsValid) { // Return True : Valid // Return False : No valid ViewBag.Title = model.CustomerID == "" ? "Add New Customer" : "Edit Customer"; return(View(model)); } // DB // TODO if (Request.Form["typeInput"] == "Add New Customer") { string customerID = CatalogBLL.Customer_Add(model); return(RedirectToAction("Index")); } else { bool updateResult = CatalogBLL.Customer_Update(model); return(RedirectToAction("Index")); } } catch (Exception ex) { ModelState.AddModelError("", ex.Message + " : " + ex.StackTrace); return(View(model)); } }
public ActionResult Input(Customer data) { try { #region Allow Null for Columns if (string.IsNullOrEmpty(data.CompanyName)) { data.CompanyName = ""; } if (string.IsNullOrEmpty(data.ContactName)) { data.ContactName = ""; } if (string.IsNullOrEmpty(data.ContactTitle)) { data.ContactTitle = ""; } if (string.IsNullOrEmpty(data.Address)) { data.Address = ""; } if (string.IsNullOrEmpty(data.City)) { data.City = ""; } if (string.IsNullOrEmpty(data.Country)) { data.Country = ""; } if (string.IsNullOrEmpty(data.Phone)) { data.Phone = ""; } if (string.IsNullOrEmpty(data.Fax)) { data.Fax = ""; } #endregion Allow Null for Columns if (!ModelState.IsValid) { return(View(data)); } Customer customer = CatalogBLL.GetCustomer(data.CustomerID); if (customer == null) { int customerId = CatalogBLL.Customer_Add(data); return(RedirectToAction("Index")); } else { bool updateResult = CatalogBLL.Customer_Update(data); return(RedirectToAction("Index")); } } catch (Exception ex) { ModelState.AddModelError("", ex.Message + ":" + ex.StackTrace); return(View(data)); } }