2
\$\begingroup\$

I have a single controller that has two methods. One generates a PDF preview [GeneratePDFPreview] and then ultimately an actual PDF document [GeneratePDF]. It uses the Rotativa library to generate the PDF via partial views. I feel like I'm repeating myself majorly with the case statements on both methods.

The reason for using case statements is because each PDF document correlates to a individual partial view. The partial views are named _[CompanyName].cshtml (IE: _WilcoxFarms.cshtml,_CrotchDusterAg.cshtml ect).

PartialView("~/Views/Pricing/PricingTemplates/_xxxxFarms.cshtml", resultSet);

I was thinking I could go to the DB and grab the company name and insert it into a string path for the location for the partial view? This would be important because the amount of templates could grow over time. I don't want to statically define things using a case statement. Here is the code:

public ActionResult GeneratePDFPreview(string PBID, string company, int zone)
{   //Fetch data
    var resultSet = pricingRepo.GetUserSpecifiedItemPricing(PBID, zone);
    //Return proper custom company template defined by user selection
    switch (company)
    {
        case "85":
            return PartialView("~/Views/Pricing/PricingTemplates/_yyyyFarms.cshtml", resultSet);
        case "2":
            return PartialView("~/Views/Pricing/PricingTemplates/_xxxxFarms.cshtml", resultSet);
    }
    return PartialView("~/Views/Pricing/PricingTemplates/_Error.cshtml");
}
public ActionResult GeneratePDF(string PBID, string company, int zone)
{   //Fetch data
    var resultSet = pricingRepo.GetUserSpecifiedItemPricing(PBID, zone);
    //Create PDF footer
    string footer = "--footer-right \"Date: [date] [time]\" " + "--footer-center \"Page: [page] of [toPage]\" --footer-line --footer-font-size \"9\" --footer-spacing 5 --footer-font-name \"calibri light\"";
    switch (company)
    {
        case "85":
            return new Rotativa.PartialViewAsPdf("~/Views/Pricing/PricingTemplates/_yyyyFarms.cshtml", resultSet) {
                FileName = "yyyyFarms" + DateTime.Now + ".pdf",
                CustomSwitches = footer
            };
        case "2":
            return new Rotativa.PartialViewAsPdf("~/Views/Pricing/PricingTemplates/_xxxxFarms.cshtml", resultSet)
            {
                FileName = "xxxxFarms" + DateTime.Now + ".pdf",
                CustomSwitches = footer
            };
    }
    return Content("Invalid template selection. Contact support.");

}

Maybe you folks have a more clever idea?

[Edit] Here is my final idea:

public ActionResult GeneratePDFPreview(string PBID, int company, int zone)
        {   //Fetch data

            var resultSet = pricingRepo.GetUserSpecifiedItemPricing(PBID, zone, company);
            if (PartialViewExists(resultSet.CompanyDesc) == true)
            {
                return PartialView($"~/Views/Pricing/PricingTemplates/_{resultSet.CompanyDesc}.cshtml", resultSet);
            }

            return PartialView("~/Views/Pricing/PricingTemplates/_Error.cshtml");
        }
        public ActionResult GeneratePDF(string PBID, int company, int zone)
        {   //Fetch data
            var resultSet = pricingRepo.GetUserSpecifiedItemPricing(PBID, zone, company);

            if (PartialViewExists(resultSet.CompanyDesc) == true)
            {//Create PDF footer
                string footer = "--footer-right \"Date: [date] [time]\" " + "--footer-center \"Page: [page] of [toPage]\" --footer-line --footer-font-size \"9\" --footer-spacing 5 --footer-font-name \"calibri light\"";
                return new Rotativa.PartialViewAsPdf($"~/Views/Pricing/PricingTemplates/_{resultSet.CompanyDesc}.cshtml", resultSet)
                {
                    FileName = resultSet.CompanyDesc + DateTime.Now + ".pdf",
                    CustomSwitches = footer
                };
            }
            return Content("Invalid template selection. Contact support.");
        }
        //Used to make sure PDF template is created
        private bool PartialViewExists(string name)
        {
            ViewEngineResult result = ViewEngines.Engines.FindView(ControllerContext, "_" + name, null);
            return (result.View != null);
        }
\$\endgroup\$
3
  • \$\begingroup\$ You are speaking about both controllers but I can see only two methods (Actions). Is this what you mean? \$\endgroup\$
    – t3chb0t
    Commented Jun 21, 2018 at 18:01
  • \$\begingroup\$ @t3chb0t Yes! My mistake \$\endgroup\$
    – jon.r
    Commented Jun 21, 2018 at 18:28
  • 4
    \$\begingroup\$ Now that you posted another idea I'm even more confused then before. Is this working code? Do you now have two versions or only one? Could you clarify this or clean it up? ;-) \$\endgroup\$
    – t3chb0t
    Commented Jun 21, 2018 at 19:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.