// GET: SupSetting public ActionResult Index() { var userId = User.Identity.Name; var userData = UserManager.Users.Where(x => x.UserName == userId).SingleOrDefault(); var supData = db.SupplierAccount.Where(x => x.SupplierAccountID == userId).SingleOrDefault(); var supInfoData = db.SupplierInfo.Where(x => x.SupplierCode == supData.SupplierCode).SingleOrDefault(); Sup_parent m = new Sup_parent(); SupInfoViewModel info = new SupInfoViewModel(); SupSettingViewModel sup = new SupSettingViewModel(); info.SupplierName = supInfoData.SupplierName; info.TaxID = supInfoData.TaxID; info.Tel = supInfoData.Tel; info.Address = supInfoData.Address; info.Email = supInfoData.Email; sup.SupplierAccountID = User.Identity.Name; sup.ContactName = userData.RealName; sup.Email = userData.Email; sup.Tel = supData.Tel; sup.Mobile = supData.Mobile; sup.EnableTwoFactorAuth = userData.TwoFactorEnabled; m.SupInfoViewModel = info; m.SupSettingViewModel = sup; return(View(m)); }
public async Task <ActionResult> Index(Sup_parent m) { try { var userId = User.Identity.Name; var userData = UserManager.Users.Where(x => x.UserName == userId).SingleOrDefault(); var supData = db.SupplierAccount.Where(x => x.SupplierAccountID == userId).SingleOrDefault(); var supInfoData = db.SupplierInfo.Where(x => x.SupplierCode == supData.SupplierCode).SingleOrDefault(); SupInfoViewModel info = new SupInfoViewModel(); SupSettingViewModel sup = new SupSettingViewModel(); //姓名 userData.RealName = m.SupSettingViewModel.ContactName; supData.ContactName = m.SupSettingViewModel.ContactName; //信箱 string holder = userData.Email; userData.Email = m.SupSettingViewModel.Email; supData.Email = m.SupSettingViewModel.Email; //如果信箱有改,驗證要重置為false if (holder != m.SupSettingViewModel.Email) { userData.EmailConfirmed = false; } //手機 userData.PhoneNumber = m.SupSettingViewModel.Mobile; supData.Mobile = m.SupSettingViewModel.Mobile; //市話 supData.Tel = m.SupSettingViewModel.Tel; //雙因素 //判斷Email是否通過驗證 userData.TwoFactorEnabled = m.SupSettingViewModel.EnableTwoFactorAuth; var r = await UserManager.UpdateAsync(userData); db.Entry(supData).State = System.Data.Entity.EntityState.Modified; var r2 = await db.SaveChangesAsync(); if (r.Succeeded && r2 > 0) { TempData["SuccessMsg"] = "修改成功!"; return(RedirectToAction("Index")); } TempData["ErrorMsg"] = "修改失敗,請檢查網路連線再試一次。"; return(RedirectToAction("Index")); } catch { TempData["ErrorMsg"] = "修改失敗,不可空值,並請檢查網路連線再試一次..."; return(RedirectToAction("Index")); } }
public async Task <ActionResult> IndexRight(Sup_parent m) { try { var userId = User.Identity.Name; var userData = UserManager.Users.Where(x => x.UserName == userId).SingleOrDefault(); var supData = db.SupplierAccount.Where(x => x.SupplierAccountID == userId).SingleOrDefault(); var supInfoData = db.SupplierInfo.Where(x => x.SupplierCode == supData.SupplierCode).SingleOrDefault(); SupInfoViewModel info = new SupInfoViewModel(); //供應商名稱 supInfoData.SupplierName = m.SupInfoViewModel.SupplierName; //統一編號 supInfoData.TaxID = m.SupInfoViewModel.TaxID; //公司信箱 supInfoData.Email = m.SupInfoViewModel.Email; //公司市話 supInfoData.Tel = m.SupInfoViewModel.Tel; //公司地址 supInfoData.Address = m.SupInfoViewModel.Address; db.Entry(supData).State = System.Data.Entity.EntityState.Modified; var r2 = await db.SaveChangesAsync(); if (r2 > 0) { TempData["SuccessMsg"] = "修改成功!"; return(RedirectToAction("Index")); } TempData["ErrorMsg"] = "修改失敗,請檢查網路連線再試一次。"; return(RedirectToAction("Index")); } catch { TempData["ErrorMsg"] = "修改失敗,不可空值,並請檢查網路連線再試一次..."; return(RedirectToAction("Index")); } }
public ActionResult Create(SupInfoViewModel m) { try { if (ModelState.IsValid) { var maxThanOID = db.SupplierInfo.Select(x => x.SupplierInfoOID).Max() + 1; string SupCodestr = String.Format("S{0:00000}", Convert.ToDouble(maxThanOID)); var supInfo = db.SupplierInfo; SupplierInfo s = new SupplierInfo(); s.SupplierCode = SupCodestr; s.SupplierName = m.SupplierName; s.TaxID = m.TaxID; s.Address = m.Address; s.Email = m.Email; s.Tel = m.Tel; s.SupplierRatingOID = m.SupplierRatingOID; supInfo.Add(s); var result = db.SaveChanges(); if (result > 0) { TempData["Success"] = $"{s.SupplierName} 更新成功"; return(View("Index")); } else { ViewBag.Error = "對不起,伺服器發生錯誤,請再試一次。"; return(View(m)); } } } catch (Exception ex) { ViewBag.Error = $"對不起,伺服器發生錯誤: {ex.Message},請再試一次。"; } return(View(m)); }
public ActionResult CreateSupInfo(SupInfoViewModel SupInfoModel) { //if (!ModelState.IsValid) //{ // BuyerSupAcc_Parent p = new BuyerSupAcc_Parent(); // p.SupInfoModel = SupInfoModel; // return View("Create", p); //} //檢查是否有公司可選 var result = getAllSupInfoNoContactOnlySupInfoToIndexAjax().Data; var data = JsonConvert.SerializeObject(result); if (data == "[]") { try { //supInfo var maxsupThanOID = db.SupplierInfo.Select(x => x.SupplierInfoOID).Max() + 1; string SupCodestr = String.Format("S{0:00000}", Convert.ToDouble(maxsupThanOID)); SupplierInfo supinfo = new SupplierInfo(); supinfo.SupplierCode = SupCodestr; supinfo.SupplierName = SupInfoModel.SupplierName; supinfo.TaxID = SupInfoModel.TaxID; supinfo.Tel = SupInfoModel.Tel; supinfo.Email = SupInfoModel.Email; supinfo.Address = SupInfoModel.Address; supinfo.SupplierRatingOID = null; db.SupplierInfo.Add(supinfo); var r1 = db.SaveChanges(); if (r1 > 0) { //新增公司後回到view TempData["Success"] = $"{supinfo.SupplierName} 更新成功"; return(RedirectToAction("Create")); } else { ModelState.AddModelError("", "填寫欄位有錯誤"); BuyerSupAcc_Parent p = new BuyerSupAcc_Parent(); p.SupInfoModel = SupInfoModel; return(View("Create", p)); } } catch (DbEntityValidationException ex) { var entityError = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage); var getFullMessage = string.Join("; ", entityError); var exceptionMessage = string.Concat(ex.Message, "errors are: ", getFullMessage); Console.WriteLine(exceptionMessage); if (!ModelState.IsValid) { BuyerSupAcc_Parent p = new BuyerSupAcc_Parent(); p.SupInfoModel = SupInfoModel; return(View("Create", p)); } } } //新增公司後回到view TempData["Success"] = "更新成功"; return(RedirectToAction("Create")); }