从过滤数据MVC5填充PDF字段

时间:2017-08-04 13:44:11

标签: asp.net-mvc

我有一个包含一些信息的数据库,我为它做了简单的过滤。我还为每个循环创建了一个类,用这个信息填充pdf字段。我试图弄清楚如何使用过滤器数据填充相同的字段。例如,我想与我过滤的特定城市的人们制作PDF文件。

 //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) && !String.IsNullOrWhiteSpace(numAgente))
        {
            filterRecord = filterRecord.Where(r => r.PPOLIZ.Contains(numPoliza));
        }

        return View(filterRecord);

    }

这是填充字段的类。现在,它填充字段并为数据库中的所有条目创建PDF。我需要它来填充并仅从过滤后的数据创建PDF。

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");
                }

            }

        }




    } 

我只是在FilterRecords操作方法上添加if语句吗?但是,如果r在其他方法上,我如何访问r?

    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 => r.SUCUR.Contains(sucursal));
        }


      return View(filteredRecords);
}

1 个答案:

答案 0 :(得分:0)

我想,在您的网站上显示FilterRecord的结果之后,稍后会调用您的CreateVoucher()方法。 如果这是真的,我建议您将过滤器参数存储在用户会话中,并在用户请求时将其传递给您的CreateVoucher()方法。

不是在CreateVoucher()中执行数据库查询,而是最好调用一种特殊方法 - 例如RequestRecords(...) - 在Action和CreateVoucher()中执行所有数据查询工作。

private IEnumerable<string> RequestRecords(string sucursal, string numPoliza, string numAgente, string numCertificado) 
{
    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).ToList();
    if (!String.IsNullOrWhiteSpace(sucursal))
    {
        filterRecord = filterRecord.Where(r => r.SUCUR.Contains(sucursal));
    }

    return filteredRecord;
}

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;
      ...

      return View(filteredRecords);
}

public void CreateVoucher(string sucursal, string numPoliza, string numAgente, string numCertificado)
{
     var filteredRecords = RequestRecords(sucursal, ...);
     foreach( var record in filteredRecords)
     {
          // your pdf creation stuff 
     }
}