0

So I have a list of string that contains email addresses. Its getting populated fine. But when I pass the list named "mails" to another function, the receiving function shows the count of the list to be 1. Which means list is somehow losing all its entries except for either 1st or the last one. Following are the two functions.

public ActionResult SendMail(int? id)
    {
        if (ModelState.IsValid)
        {
          
            var students = db.Students.ToList();

            List<String> mails = new List<String>();
            

            foreach (var item in students)
            {
                
                if (Request.Form[item.St_id.ToString()] == null)
                {
                    continue;
                }
                mails.Add(item.St_email);

            }
            return RedirectToAction("SendPDF2", new { mails= mails });     

        }
        return RedirectToAction("Index", "Students");
        
    }

The above function sends the list called mails to the function called SendPDF2. Below is the function SendPDF2 which recieves the list.

public ActionResult SendPDF2(List<String> mails)
    {
        var timetables = db.Timetables.Include(t => t.Class).Include(t => t.Cours).Include(t => t.Teacher).OrderBy(t => t.Ti_id);
        
        string[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
        //ViewData["days"] = days;
        ViewBag.Days = days.ToArray();
        string[] time = { "9am", "10am", "11am", "12pm", "1pm" };
        ViewBag.Time = time.ToArray();


        List<Timetable> timetable = db.Timetables.ToList();
        var pdf = new ViewAsPdf(timetable)
        {
            FileName = "TestPartialViewAsPdf.pdf"
        };
        var myPdfAsBytes = pdf.BuildFile(this.ControllerContext);

        foreach (var maili in mails)
        {
            using (MailMessage mail = new MailMessage())
            {
                //var ma = "syedzaidi7684@gmail.com";
                mail.From = new MailAddress("razaabbas768@gmail.com");
                mail.To.Add(new MailAddress(maili));
                mail.Subject = "GMA Timetable";
                mail.Body = "<h2>Timetable</h2>";
                mail.IsBodyHtml = true;
                mail.Attachments.Add(new Attachment(new MemoryStream(myPdfAsBytes), "Timetable.pdf"));

                using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
                {
                    smtp.Credentials = new NetworkCredential("razaabbas768@gmail.com", "password");
                    smtp.EnableSsl = true;
                    smtp.Send(mail);
                }
            }

        }
        return RedirectToAction("Index", "Classes");

1 Answers1

0

Instead of using RedirectToAction() to pass the list, you can simply extract the logic inside SendPDF2() like this:

private void SendTimeTables(List<string> mails)
{
    /* the code originally in SendPDF2()... */
}

After that, both SendMail() and SendPDF2() call SendTimeTables() to send emails:

public ActionResult SendMail (int? id) {
    if (ModelState.IsValid) {
        var students = db.Students.ToList();
        List<String> mails = new List<String>();
        /* steps to create mails... */
        SendTimeTables(mails);
        return RedirectToAction ("Index", "Classes");
    }
    return RedirectToAction ("Index", "Students");
}

public ActionResult SendPDF2 (List<String> mails) {
    SendTimeTables(mails);
    return RedirectToAction ("Index", "Classes");
}

In order to pass a List<string> from one controller action to another, you may need to make a POST call, which it is not RedirectToAction() can do (some discussions here).

Bemn
  • 1,291
  • 1
  • 7
  • 22