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"));
     }
 }
示例#2
0
        public void Save(UnitEntity unit, ContactInfoEntity contactInfo)
        {
            var db = DatabaseFactory.CreateDatabase();

            using (var conn = db.CreateConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        if (string.IsNullOrEmpty(contactInfo.Id))
                        {
                            contactInfo.Id = Guid.NewGuid().ToString();
                            ContactInfoRepository.Add(contactInfo, db, trans);
                        }
                        else
                        {
                            ContactInfoRepository.Update(contactInfo, db, trans);
                        }

                        unit.ContactId = contactInfo.Id;
                        if (string.IsNullOrEmpty(unit.Id))
                        {
                            unit.Id = Guid.NewGuid().ToString();
                            if (unit.Type == UnitType.Hospital || unit.Type == UnitType.Vendor)
                            {
                                unit.RootId   = Constant.DEFAULT_UNIT_ROOT_ID;
                                unit.ParentId = unit.Id;
                            }

                            UnitRepository.Add(unit, db, trans);
                        }
                        else
                        {
                            UnitRepository.Update(unit, db, trans);
                            ClearCache(unit.Id);
                        }

                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        throw;
                    }
                }
            }
        }
 public ActionResult Delete(long lContactInfoID, FormCollection objFormCollection)
 {
     try
     {
         ContactInfoRepository objContactInfoRepository = new ContactInfoRepository();
         ContactInfoData       objContactInfoData       = objContactInfoRepository.GetContactInfo(lContactInfoID);
         if (null != objContactInfoData)
         {
             objContactInfoRepository.DeleteContactInfo(objContactInfoData);
             return(RedirectToAction("Index"));
         }
         else
         {
             ModelState.AddModelError("DeleteError", "刪除失敗!");
             return(View(objContactInfoData));
         }
     }
     catch
     {
         ModelState.AddModelError("DeleteError", "刪除失敗!");
         return(View());
     }
 }
 public ActionResult Edit(long lContactInfoID, FormCollection objFormCollection)
 {
     try
     {
         ContactInfoRepository objContactInfoRepository = new ContactInfoRepository();
         ContactInfoData       objContactInfoData       = objContactInfoRepository.GetContactInfo(lContactInfoID);
         if (null != objContactInfoData && TryUpdateModel <ContactInfoData>(objContactInfoData, "", objFormCollection.AllKeys, new string[] { "ContactInfoID", "CreateTime", "UpdateTime" }))
         {
             objContactInfoRepository.UpdateContactInfo(objContactInfoData);
             return(RedirectToAction("Index"));
         }
         else
         {
             ModelState.AddModelError("UpdateError", "更新失敗!");
             return(View(objContactInfoData));
         }
     }
     catch
     {
         ModelState.AddModelError("UpdateError", "更新失敗!");
         return(View());
     }
 }