public async Task <int> Create(Contact model) { do { model.Code = CodeGenerator.Generate(); }while (this.DbSet.Any(d => d.Code.Equals(model.Code))); model.Company = null; ContactLogic.Create(model); return(await DbContext.SaveChangesAsync()); }
public ActionResult Public(ContactViewModel model) { string userResponse = HttpContext.Request.Params["g-recaptcha-response"]; bool validCaptcha = ReCaptcha.ValidateCaptcha(userResponse); if (validCaptcha) { try { Contact contact = new Contact() { Address = model.Address, Content = model.Content, Email = model.Email, Name = model.Name, Title = model.Title }; var response = _contactLogic.Create(contact); if (response.IsError == true) { foreach (var item in response.ErrorCodes) { ModelState.AddModelError(string.Empty, item); } return(View(model)); } else { var msg = new MailMessage(); msg.To.Add(new MailAddress(ConfigurationManager.AppSettings["AdminEmail"])); msg.Subject = "E-SINERGI Contact - " + model.Title; msg.Body = "Dari: " + model.Email + ", Isi: " + model.Content; msg.From = new MailAddress("*****@*****.**"); using (var client = new SmtpClient() { Host = "relay-hosting.secureserver.net", Port = 25, EnableSsl = false, UseDefaultCredentials = false }) { client.Send(msg); } } return(RedirectToAction("ThankYouPage")); } catch (Exception ex) { return(View()); } } else { ModelState.AddModelError(string.Empty, "Invalid Captcha"); return(View()); } }