public ActionResult AddCustomer(string Code,string Name1,string Name2,string SimpleName,string ContactPerson, string Telephone, string Fax, string Email, string Address, string MailCode, string Remark) { this.Internationalization(); try { TugDataEntities db = new TugDataEntities(); System.Linq.Expressions.Expression<Func<Customer, bool>> exp = u => u.Name1 == Name1; Customer obj = db.Customer.Where(exp).FirstOrDefault(); if (obj != null) { throw new Exception("客户名称已存在!"); } { TugDataModel.Customer cstmer = new Customer(); cstmer.Code = Code; cstmer.Name1 = Name1; cstmer.Name2 = Name2; cstmer.SimpleName = SimpleName; cstmer.ContactPerson = ContactPerson; cstmer.Telephone = Telephone; cstmer.Fax = Fax; cstmer.Email = Email; cstmer.Address = Address; cstmer.MailCode = MailCode; cstmer.Remark = Remark; cstmer.OwnerID = -1; cstmer.CreateDate = cstmer.LastUpDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); ;//.ToString("yyyy-MM-dd"); cstmer.UserID = Session.GetDataFromSession<int>("userid"); cstmer.UserDefinedCol1 = ""; cstmer.UserDefinedCol2 = ""; cstmer.UserDefinedCol3 = ""; cstmer.UserDefinedCol4 = ""; //if (Request.Form["UserDefinedCol5"].Trim() != "") // cstmer.UserDefinedCol5 = Convert.ToDouble(Request.Form["UserDefinedCol5"].Trim()); //if (Request.Form["UserDefinedCol6"].Trim() != "") // cstmer.UserDefinedCol6 = Util.toint(Request.Form["UserDefinedCol6"].Trim()); //if (Request.Form["UserDefinedCol7"].Trim() != "") // cstmer.UserDefinedCol7 = Util.toint(Request.Form["UserDefinedCol7"].Trim()); //if (Request.Form["UserDefinedCol8"].Trim() != "") // cstmer.UserDefinedCol8 = Util.toint(Request.Form["UserDefinedCol8"].Trim()); cstmer.UserDefinedCol9 = ""; cstmer.UserDefinedCol10 = ""; cstmer = db.Customer.Add(cstmer); db.SaveChanges(); var ret = new { code = Resources.Common.SUCCESS_CODE, message = Resources.Common.SUCCESS_MESSAGE }; //Response.Write(@Resources.Common.SUCCESS_MESSAGE); return Json(ret); } } catch (Exception ex) { throw ex; } }
public ActionResult AddEdit() { this.Internationalization(); #region Add if (Request.Form["oper"].Equals("add")) { try { TugDataEntities db = new TugDataEntities(); { TugDataModel.Customer cstmer = new Customer(); cstmer.Code = Request.Form["Code"]; cstmer.Name1 = Request.Form["Name1"]; cstmer.Name2 = Request.Form["Name2"]; cstmer.SimpleName = Request.Form["SimpleName"]; cstmer.TypeID = Util.toint(Request.Form["TypeID"]); cstmer.ContactPerson = Request.Form["ContactPerson"]; cstmer.Telephone = Request.Form["Telephone"]; cstmer.Fax = Request.Form["Fax"]; cstmer.Email = Request.Form["Email"]; cstmer.Address = Request.Form["Address"]; cstmer.MailCode = Request.Form["MailCode"]; cstmer.Remark = Request.Form["Remark"]; cstmer.OwnerID = -1; cstmer.CreateDate = cstmer.LastUpDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");;//.ToString("yyyy-MM-dd"); cstmer.UserID = Session.GetDataFromSession<int>("userid"); cstmer.UserDefinedCol1 = Request.Form["UserDefinedCol1"]; cstmer.UserDefinedCol2 = Request.Form["UserDefinedCol2"]; cstmer.UserDefinedCol3 = Request.Form["UserDefinedCol3"]; cstmer.UserDefinedCol4 = Request.Form["UserDefinedCol4"]; if (Request.Form["UserDefinedCol5"] != "") cstmer.UserDefinedCol5 = Convert.ToDouble(Request.Form["UserDefinedCol5"]); if (Request.Form["UserDefinedCol6"] != "") cstmer.UserDefinedCol6 = Util.toint(Request.Form["UserDefinedCol6"]); if (Request.Form["UserDefinedCol7"] != "") cstmer.UserDefinedCol7 = Util.toint(Request.Form["UserDefinedCol7"]); if (Request.Form["UserDefinedCol8"] != "") cstmer.UserDefinedCol8 = Util.toint(Request.Form["UserDefinedCol8"]); cstmer.UserDefinedCol9 = Request.Form["UserDefinedCol9"]; cstmer.UserDefinedCol10 = Request.Form["UserDefinedCol10"]; cstmer = db.Customer.Add(cstmer); db.SaveChanges(); var ret = new { code = Resources.Common.SUCCESS_CODE, message = Resources.Common.SUCCESS_MESSAGE }; Response.Write(@Resources.Common.SUCCESS_MESSAGE); return Json(ret); } } catch (Exception) { var ret = new { code = Resources.Common.EXCEPTION_CODE, message = Resources.Common.EXCEPTION_MESSAGE }; //Response.Write(@Resources.Common.EXCEPTION_MESSAGE); return Json(ret); } } #endregion Add #region Edit if (Request.Form["oper"].Equals("edit")) { try { int idx = Util.toint(Request.Form["IDX"]); string name1 = Request.Form["Name1"]; TugDataEntities db = new TugDataEntities(); System.Linq.Expressions.Expression<Func<Customer, bool>> exp = u => u.Name1 == name1 && u.IDX!=idx; Customer obj = db.Customer.Where(exp).FirstOrDefault(); if (obj != null) { return Json(new { code = Resources.Common.ERROR_CODE, message = "客户名称已存在!" });//Resources.Common.ERROR_MESSAGE } Customer cstmer = db.Customer.Where(u => u.IDX == idx).FirstOrDefault(); if (cstmer == null) { return Json(new { code = Resources.Common.ERROR_CODE, message = Resources.Common.ERROR_MESSAGE }); } else { cstmer.Code = Request.Form["Code"]; cstmer.Name1 = Request.Form["Name1"]; cstmer.Name2 = Request.Form["Name2"]; cstmer.SimpleName = Request.Form["SimpleName"]; cstmer.TypeID =Util.toint(Request.Form["TypeID"]); cstmer.ContactPerson = Request.Form["ContactPerson"]; cstmer.Telephone = Request.Form["Telephone"]; cstmer.Fax = Request.Form["Fax"]; cstmer.Email = Request.Form["Email"]; cstmer.Address = Request.Form["Address"]; cstmer.MailCode = Request.Form["MailCode"]; cstmer.Remark = Request.Form["Remark"]; cstmer.OwnerID = -1; cstmer.LastUpDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");; cstmer.UserID = Session.GetDataFromSession<int>("userid"); cstmer.UserDefinedCol1 = Request.Form["UserDefinedCol1"]; cstmer.UserDefinedCol2 = Request.Form["UserDefinedCol2"]; cstmer.UserDefinedCol3 = Request.Form["UserDefinedCol3"]; cstmer.UserDefinedCol4 = Request.Form["UserDefinedCol4"]; if (Request.Form["UserDefinedCol5"] != "") cstmer.UserDefinedCol5 = Convert.ToDouble(Request.Form["UserDefinedCol5"]); if (Request.Form["UserDefinedCol6"] != "") cstmer.UserDefinedCol6 = Util.toint(Request.Form["UserDefinedCol6"]); if (Request.Form["UserDefinedCol7"] != "") cstmer.UserDefinedCol7 = Util.toint(Request.Form["UserDefinedCol7"]); if (Request.Form["UserDefinedCol8"] != "") cstmer.UserDefinedCol8 = Util.toint(Request.Form["UserDefinedCol8"]); cstmer.UserDefinedCol9 = Request.Form["UserDefinedCol9"]; cstmer.UserDefinedCol10 = Request.Form["UserDefinedCol10"]; db.Entry(cstmer).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); var orderList = db.OrderInfor.Where(u => u.CustomerID == idx).ToList(); if (orderList != null) { foreach (var item in orderList) { item.CustomerName = cstmer.Name1; db.Entry(item).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } } return Json(new { code = Resources.Common.SUCCESS_CODE, message = Resources.Common.SUCCESS_MESSAGE }); } } catch (Exception ex) { throw ex; } } #endregion Edit return Json(new { code = Resources.Common.ERROR_CODE, message = Resources.Common.ERROR_MESSAGE }); }