title: Populate PDF fields from filtered data MVC5
body: <p>I'm have a database with some info and i made simple filter for it. I also made a class with a for each loop to populate pdf fields with that info. I'm trying to figure out how populate the same fields just with the filter data. For example i want to make PDF's with people of a specific city i filtered.</p>
  
  <pre><code> //this is the filter.
  public  ActionResult FilterReccords (string sucursal, string numPoliza, string numAgente, string numCertificado)
      {
          ViewBag.SUCUR = (from r in db.JUXLSY select r.SUCUR).Distinct();
          var filterRecord = from r in db.JUXLSY
                             orderby r.NOMBRE_AGENTE
                             where r.SUCUR == sucursal || sucursal == null || sucursal == ""
                             where r.PPOLIZ == numPoliza || numPoliza == null || numPoliza == ""
                             where r.AGENTE == numAgente || numAgente == null || numAgente == ""
  
                             select r;
          if (!String.IsNullOrWhiteSpace(numPoliza) &amp;&amp; !String.IsNullOrWhiteSpace(numAgente))
          {
              filterRecord = filterRecord.Where(r =&gt; r.PPOLIZ.Contains(numPoliza));
          }
  
          return View(filterRecord);
  
      }
  </code></pre>
  
  <p>This is the class that populate the fields. For now it populates the fields and creates a PDF for all the entries in the Database. I need it to populate and create a PDF from only the filtered data.</p>
  
  <pre><code>public void CreateVoucher()
      {
          Aspose.Pdf.License license = new Aspose.Pdf.License();
          license.SetLicense("Aspose.Pdf.lic");
          Aspose.Pdf.Facades.Form pdfForm = new Aspose.Pdf.Facades.Form();
          var output = Server.MapPath("/PrintingOutput/");
  
          using (var db = new db_VouchersEntities())
          {
  
              var voucher = db.JUXLSY.ToList();
  
              foreach (var vouch in voucher)
              {
  
                  pdfForm.BindPdf(Server.MapPath("/VoucherTemplate/VoucherCertificateTemplateForm.pdf"));
                  pdfForm.FillField("topmostSubform[0].Page1[0].AseguradoraTxt[0]", vouch.SUCUR);
  
                  if (Directory.Exists(output) == false)
                  {
                      Directory.CreateDirectory(output);
                      pdfForm.FlattenAllFields();
                      pdfForm.Save(output + vouch.SUCUR.ToString().Trim() + '_' + vouch.NOMBRE_AGENTE.ToString().Trim() + '_' + vouch.PCERNO.ToString().Trim() + ".pdf");
                  }
                  else
                  {
                      pdfForm.FlattenAllFields();
                      pdfForm.Save(output + vouch.SUCUR.ToString().Trim() + '_' + vouch.NOMBRE_AGENTE.ToString().Trim() + '_' + vouch.PCERNO.ToString().Trim() + ".pdf");
                  }
  
              }
  
          }
  
  
  
  
      } 
  </code></pre>
  
  <p>Do i just add the if statement on the FilterRecords action method? But how do i Access the r in where if the r is on the other method</p>
  
  <pre><code>    public ActionResult FilterRecords(string sucursal, string numPoliza, 
       string numAgente, string numCertificado)
  {
        var filteredRecords = RequestRecords(sucursal, ...);
        // store your filter parameters in a session
        Session["sucursal"] = sucursal;
  
  //filter to be display to the user               
         if (!String.IsNullOrWhiteSpace(sucursal))
          {
              filterRecord = filterRecord.Where(r =&gt; r.SUCUR.Contains(sucursal));
          }
  
  
        return View(filteredRecords);
  }
  </code></pre>