Choosing which PDF preview to generate

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












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







share|improve this question





















  • You are speaking about both controllers but I can see only two methods (Actions). Is this what you mean?
    – t3chb0t
    Jun 21 at 18:01










  • @t3chb0t Yes! My mistake
    – Waragi
    Jun 21 at 18:28






  • 4




    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? ;-)
    – t3chb0t
    Jun 21 at 19:27
















up vote
2
down vote

favorite












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







share|improve this question





















  • You are speaking about both controllers but I can see only two methods (Actions). Is this what you mean?
    – t3chb0t
    Jun 21 at 18:01










  • @t3chb0t Yes! My mistake
    – Waragi
    Jun 21 at 18:28






  • 4




    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? ;-)
    – t3chb0t
    Jun 21 at 19:27












up vote
2
down vote

favorite









up vote
2
down vote

favorite











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







share|improve this question













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









share|improve this question












share|improve this question




share|improve this question








edited Jun 22 at 14:02
























asked Jun 21 at 17:28









Waragi

257110




257110











  • You are speaking about both controllers but I can see only two methods (Actions). Is this what you mean?
    – t3chb0t
    Jun 21 at 18:01










  • @t3chb0t Yes! My mistake
    – Waragi
    Jun 21 at 18:28






  • 4




    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? ;-)
    – t3chb0t
    Jun 21 at 19:27
















  • You are speaking about both controllers but I can see only two methods (Actions). Is this what you mean?
    – t3chb0t
    Jun 21 at 18:01










  • @t3chb0t Yes! My mistake
    – Waragi
    Jun 21 at 18:28






  • 4




    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? ;-)
    – t3chb0t
    Jun 21 at 19:27















You are speaking about both controllers but I can see only two methods (Actions). Is this what you mean?
– t3chb0t
Jun 21 at 18:01




You are speaking about both controllers but I can see only two methods (Actions). Is this what you mean?
– t3chb0t
Jun 21 at 18:01












@t3chb0t Yes! My mistake
– Waragi
Jun 21 at 18:28




@t3chb0t Yes! My mistake
– Waragi
Jun 21 at 18:28




4




4




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? ;-)
– t3chb0t
Jun 21 at 19:27




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? ;-)
– t3chb0t
Jun 21 at 19:27















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f196991%2fchoosing-which-pdf-preview-to-generate%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f196991%2fchoosing-which-pdf-preview-to-generate%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation