//供應商公司確認新增後>新增一供應商帳號加入> // GET: BuyerSupAccount/Create public ActionResult Create() { BuyerSupAcc_Parent m = new BuyerSupAcc_Parent(); 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")); }
public async Task <ActionResult> Create(BuyerSupAcc_Parent m, string AccStatus) { try { if (!ModelState.IsValid) { ModelState.AddModelError("", "填寫欄位有錯誤"); return(View(m)); } //================================================================================== var maxThanOID = db.SupplierAccount.Select(x => x.SupplierAccountOID).Max() + 1; string SupAccIDstr = String.Format("SE{0:00000}", Convert.ToDouble(maxThanOID)); string pwd = generateFirstPwd(); string hashpwd = UserManager.PasswordHasher.HashPassword(pwd); //sa SupplierAccount sa = new SupplierAccount(); sa.SupplierCode = m.BuyerSupAccount_CreateViewModel.SupplierCode; sa.SupplierAccountID = SupAccIDstr; sa.ContactName = m.BuyerSupAccount_CreateViewModel.ContactName; sa.PasswordHash = hashpwd; //此欄位無用但非null sa.PasswordSalt = "fd357578-7784-4dea-b8c1-4d8b8d290d55"; sa.Email = m.BuyerSupAccount_CreateViewModel.Email; sa.Address = null; sa.Mobile = m.BuyerSupAccount_CreateViewModel.Mobile; sa.Tel = m.BuyerSupAccount_CreateViewModel.Tel; sa.AccountStatus = m.BuyerSupAccount_CreateViewModel.AccountStatus ? "R" : "D"; sa.CreateDate = DateTime.Now; sa.CreatorEmployeeID = User.Identity.GetEmployee().EmployeeID; sa.ModifiedDate = DateTime.Now; sa.SendLetterStatus = null; sa.SendLetterDate = null; //user ApplicationUser user = new ApplicationUser(); user.Id = Guid.NewGuid().ToString(); user.Email = m.BuyerSupAccount_CreateViewModel.Email; user.PasswordHash = hashpwd; user.PhoneNumber = m.BuyerSupAccount_CreateViewModel.Mobile; user.UserName = SupAccIDstr; user.RealName = m.BuyerSupAccount_CreateViewModel.ContactName; user.LastPasswordChangedDate = null; var r1 = UserManager.Create(user); db.SupplierAccount.Add(sa); //var r2 = UserManager.Update(user); if (r1.Succeeded) { //確定聯絡人信箱沒重複才可存到Acctable var r2 = db.SaveChanges(); //判斷是否要寄信 補寄信 if (AccStatus == "on") { await sendMailatIndex(user, user.UserName); } TempData["Success"] = $"更新成功"; return(View("Index")); } else { var err = string.Join(",", r1.Errors); ModelState.AddModelError("", err); return(View("Create")); } } catch (DbEntityValidationException ex) { foreach (DbEntityValidationResult item in ex.EntityValidationErrors) { // Get entry DbEntityEntry entry = item.Entry; string entityTypeName = entry.Entity.GetType().Name; // Display or log error messages foreach (DbValidationError subItem in item.ValidationErrors) { string message = string.Format("Error '{0}' occurred in {1} at {2}", subItem.ErrorMessage, entityTypeName, subItem.PropertyName); Console.WriteLine(message); } } TempData["Error"] = "對不起,新增失敗,請檢查網路連線再重試一次"; return(View()); } }