public virtual async Task <IActionResult> Reply(int?id) { if (id == null) { return(NotFound()); } var model = await _context.GetByIdAsync <Contact>(id); var viewModel = new ContactReplyViewModel(); viewModel.ContactId = id.Value; viewModel.Contact = model; viewModel.DateSent = DateTime.Now; return(View(viewModel)); }
public virtual async Task <IActionResult> Reply(int?id, ContactReplyViewModel model) { string subject; string returnEmail; if (id == null) { return(NotFound()); } if (model == null) { return(NotFound()); } if (model.Contact == null) { model.Contact = await _context.GetByIdAsync <Contact>(id); } if (id != model.ContactId) { return(NotFound()); } // Get current users email address var user = await _userManager.GetUserAsync(this.User); model.UserName = user.Email; // Get the subject from configuration, or use default if (!string.IsNullOrEmpty(_configuration["WebsiteContactSubject"])) { subject = _configuration["WebsiteContactSubject"]; } else { subject = "Website Contact"; } // Get the return email for when email addresses are hidden from configuration if (!string.IsNullOrEmpty(_configuration["WebsiteContactReturnEmail"])) { returnEmail = _configuration["WebsiteContactReturnEmail"]; } else { throw new KeyNotFoundException(); } // Save to the data context _context.Create <ContactReply>(new ContactReply { ContactId = model.ContactId, UserId = model.UserName, Message = model.Message }); // Send the email if (model.HideEmail != true) { await _emailSender.SendEmailAsync(model.Contact.Email, subject, model.Message, model.UserName); } else { await _emailSender.SendEmailAsync(model.Contact.Email, subject, model.Message, returnEmail); } // Mark original Contact as Handled model.Contact.Handled = true; _context.Update(model.Contact); // Save changes to db await _context.SaveAsync(); return(RedirectToAction("Admin")); }