public JsonResult Edit(EditViewModel model) { List <ErrorMsg> ErrorMsgs = new List <ErrorMsg>(); if (!SysHelper.init(model)) { ErrorMsgs.Add(new ErrorMsg() { ErrorID = "Utility", ErrorText = "使用者尚未登入" }); return(Json(new { Success = false, ErrorMsgs })); } //新增 if (String.IsNullOrEmpty(model.SID)) { //新增要輸入密碼 if (String.IsNullOrEmpty(model.PWD)) { ErrorMsgs.Add(new ErrorMsg() { ErrorID = "PWD", ErrorText = "請輸入密碼" }); } using (DBEntities db = new DBEntities()) { var existmember = db.SYS_USER.AsNoTracking().Where(x => x.EMAIL == model.EMAIL).SingleOrDefault(); if (existmember != null) { ErrorMsgs.Add(new ErrorMsg() { ErrorID = "ACCT", ErrorText = "使用者帳號已經使用" }); } } model.SID = SystemIdHelper.getNewSId(); model.HASHKEY = MemberHelper.getHashKey(); MEMBER member = new MEMBER() { SID = model.SID, CDT = DateTime.Now, MDT = DateTime.Now, CSID = SystemIdHelper.getEmptySId(), MSID = SystemIdHelper.getEmptySId(), ENABLED = model.ENABLED, EMAIL = model.EMAIL, NAME = model.NAME, FBID = model.FBID, GOOGLEID = model.GOOGLEID, HASHKEY = model.HASHKEY, PWD = MemberHelper.getHashPwd(model.PWD + model.HASHKEY), VERIFY = model.VERIFY, }; if (ErrorMsgs.Count > 0) { return(Json(new { Success = false, ErrorMsgs })); } using (DBEntities db = new DBEntities()) { try { db.MEMBERs.Add(member); db.SaveChanges(); } catch (Exception ex) { ErrorMsgs.Add(new ErrorMsg() { ErrorID = "Utility", ErrorText = ex.Message }); return(Json(new { Success = false, ErrorMsgs })); } } } //修改 else { //查詢既有資料 using (DBEntities db = new DBEntities()) { var oldInfo = db.MEMBERs.AsNoTracking().Where(x => x.SID == model.SID).Single(); MEMBER newInfo = new MEMBER() { SID = model.SID, CDT = oldInfo.CDT, MDT = DateTime.Now, CSID = oldInfo.CSID, MSID = SystemIdHelper.getEmptySId(), ENABLED = model.ENABLED, EMAIL = model.EMAIL, NAME = model.NAME, HASHKEY = oldInfo.HASHKEY, PWD = oldInfo.PWD, FBID = oldInfo.FBID, GOOGLEID = oldInfo.GOOGLEID, VERIFY = model.VERIFY, }; model.HASHKEY = MemberHelper.getHashKey(); if (!String.IsNullOrEmpty(model.PWD)) { newInfo.HASHKEY = model.HASHKEY; newInfo.PWD = MemberHelper.getHashPwd(model.PWD + model.HASHKEY); } if (ErrorMsgs.Count > 0) { return(Json(new { Success = false, ErrorMsgs })); } db.Entry(newInfo).State = EntityState.Modified; db.SaveChanges(); } } return(Json(new { Success = true })); }
public JsonResult Edit(EditViewModel model) { List <ErrorMsg> ErrorMsgs = new List <ErrorMsg>(); //新增 if (String.IsNullOrEmpty(model.SID)) { model.SID = SystemIdHelper.getNewSId(); PRODUCT product = new PRODUCT() { SID = model.SID, CDT = DateTime.Now, MDT = DateTime.Now, CSID = SystemIdHelper.getEmptySId(), MSID = SystemIdHelper.getEmptySId(), ENABLED = model.ENABLED, NAME = model.NAME, CAT_SID = model.CAT_SID, CONTENT = Encoder.HtmlEncode(model.CONTENT), DESC = model.DESC, IMG_SRC = model.IMG_SRC, PRICE = model.PRICE, }; if (ErrorMsgs.Count > 0) { return(Json(new { Success = false, ErrorMsgs })); } using (DBEntities db = new DBEntities()) { try { db.PRODUCTs.Add(product); db.SaveChanges(); } catch (Exception ex) { ErrorMsgs.Add(new ErrorMsg() { ErrorID = "Utility", ErrorText = ex.Message }); return(Json(new { Success = false, ErrorMsgs })); } } } //修改 else { //查詢既有資料 using (DBEntities db = new DBEntities()) { var oldInfo = db.PRODUCTs.AsNoTracking().Where(x => x.SID == model.SID).Single(); PRODUCT newInfo = new PRODUCT() { SID = model.SID, CDT = oldInfo.CDT, MDT = DateTime.Now, CSID = oldInfo.CSID, MSID = SystemIdHelper.getEmptySId(), ENABLED = model.ENABLED, NAME = model.NAME, CAT_SID = model.CAT_SID, CONTENT = Encoder.HtmlEncode(model.CONTENT), DESC = model.DESC, IMG_SRC = model.IMG_SRC, PRICE = model.PRICE, }; if (ErrorMsgs.Count > 0) { return(Json(new { Success = false, ErrorMsgs })); } db.Entry(newInfo).State = EntityState.Modified; db.SaveChanges(); } } return(Json(new { Success = true })); }