Inheritance: INotifyPropertyChanging, INotifyPropertyChanged
示例#1
0
 partial void DeleteCustomer(Customer instance);
示例#2
0
 partial void InsertCustomer(Customer instance);
示例#3
0
 partial void UpdateCustomer(Customer instance);
示例#4
0
        public AjaxFormResult SaveCustomer(string id, FormCollection values)
        {
            AjaxFormResult response = new AjaxFormResult();

            try
            {
                //for example
                if (string.IsNullOrEmpty(values["CompanyName"]))
                {
                    response.Success = false;
                    response.Errors.Add(new FieldError("CompanyName", "The CompanyName field is required"));
                    return response;
                }

                bool isNew = false;

                Customer customer;

                if(string.IsNullOrEmpty(id))
                {
                    if (string.IsNullOrEmpty(values["CustomerID"]))
                    {
                        response.Success = false;
                        response.Errors.Add(new FieldError("CustomerID", "The CustomerID field is required"));
                        return response;
                    }
                    
                    customer = new Customer();
                    customer.CustomerID = values["CustomerID"];
                    isNew = true;
                }
                else
                {
                    customer = (from c in this.DBContext.Customers where c.CustomerID == id select c).First();
                }
                
                customer.CompanyName = values["CompanyName"];
                customer.Address = values["Address"];
                customer.City = values["City"];
                customer.ContactName = values["ContactName"];
                customer.ContactTitle = values["ContactTitle"];
                customer.Country = values["Country"];
                customer.Fax = values["Fax"];                
                customer.Phone = values["Phone"];
                customer.PostalCode = values["PostalCode"];
                customer.Region = values["Region"];

                if(isNew)
                {
                    this.DBContext.Customers.InsertOnSubmit(customer);
                }

                this.DBContext.SubmitChanges();

                response.ExtraParams["newID"] = customer.CustomerID.ToString();
            }
            catch (Exception e)
            {
                response.Success = false;
                response.ExtraParams["msg"] = e.ToString();
            }

            return response;
        }