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