public ActionResult Create_AjaxModelPost(ContactInfoData objContactInfoData) { try { if (ModelState.IsValid) { ContactInfoRepository objContactInfoRepository = new ContactInfoRepository(); objContactInfoRepository.AddContactInfo(objContactInfoData); var returnData = new { IsSuccess = true }; return(Content(JsonConvert.SerializeObject(returnData), "application/json")); } else { ModelState.AddModelError("InsertError", "新增失敗!"); var returnData = new { IsSuccess = false, ModelStateErrors = ModelState.Where(p => p.Value.Errors.Count > 0).ToDictionary(p => p.Key, p => p.Value.Errors.Select(e => e.ErrorMessage).ToArray()) }; return(Content(JsonConvert.SerializeObject(returnData), "application/json")); } } catch (Exception ex) { var returnData = new { IsSuccess = false, ExceptionMsg = ex.Message }; return(Content(JsonConvert.SerializeObject(returnData), "application/json")); } }
public AddResponse Add(AddRequest objAddRequest) { AddResponse objAddResponse = new AddResponse(); ContactInfoRepository objContactInfoRepository = new ContactInfoRepository(); try { #region [Validation] bool bolCheckSign = Utility.CheckSHA(objAddRequest.Sign.Trim(), GetSign(objAddRequest, ApiSignKey)); if (!bolCheckSign) { objAddResponse.Result = $"{MsgFail} : Sign Validate Error"; return(objAddResponse); } #endregion #region [Logic] ContactInfoData objContactInfoData = new ContactInfoData(); objContactInfoData.Name = objAddRequest.Name.Trim(); objContactInfoData.Nickname = (!string.IsNullOrWhiteSpace(objAddRequest.Nickname) ? objAddRequest.Nickname.Trim() : null); objContactInfoData.Gender = (ContactInfoData.EnumGender?)objAddRequest.Gender; objContactInfoData.Age = objAddRequest.Age; objContactInfoData.PhoneNo = objAddRequest.PhoneNo.Trim(); objContactInfoData.Address = objAddRequest.Address.Trim(); bool bolAddResult = objContactInfoRepository.AddContactInfo(objContactInfoData); if (bolAddResult) { objAddResponse.Result = MsgSuccess; objAddResponse.Data = objContactInfoData; objAddResponse.Sign = GetSign(objAddResponse, ApiSignKey); } else { objAddResponse.Result = $"{MsgFail} : Add Data Error"; } return(objAddResponse); #endregion } catch (Exception ex) { #region [Exception] objAddResponse.Result = $"{MsgException} : {ex.Message}"; return(objAddResponse); #endregion } }
public ActionResult Create(ContactInfoData objContactInfoData) { try { if (ModelState.IsValid) { ContactInfoRepository objContactInfoRepository = new ContactInfoRepository(); objContactInfoRepository.AddContactInfo(objContactInfoData); return(RedirectToAction("Index")); } else { ModelState.AddModelError("InsertError", "新增失敗!"); return(View(objContactInfoData)); } } catch { ModelState.AddModelError("InsertError", "新增失敗!"); return(View(objContactInfoData)); } }