public async Task<ActionResult> Index() { var model = new MailRecipientsViewModel(); // Get a list of all the recipients: var recipients = await db.MailRecipients.ToListAsync(); foreach (var item in recipients) { // Put the relevant data into the ViewModel: var newRecipient = new SelectRecipientEditorViewModel() { MailRecipientId = item.MailRecipientId, FullName = item.FullName, Company = item.Company, Email = item.Email, LastMailedDate = item.getLastEmailDate().HasValue ? item.getLastEmailDate().Value.ToString("F") : "", Selected = true }; // Add to the list contained by the "wrapper" ViewModel: model.MailRecipients.Add(newRecipient); } // Pass to the view and return: return View(model); }
public async Task<ActionResult> SendMail(MailRecipientsViewModel recipients) { // Retrieve the ids of the recipients selected: var selectedIds = recipients.getSelectedRecipientIds(); // Grab the recipient records: var selectedMailRecipients = this.LoadRecipientsFromIds(selectedIds); // Build the message container for each: var messageContainers = this.createRecipientMailMessages(selectedMailRecipients); // Send the mail: var sender = new MailSender(); var sent = sender.SendMail(messageContainers); // Save a record of each mail sent: this.SaveSentMail(sent); // Reload the index form: return RedirectToAction("Index"); }