2014-04-23 31 views
1

我正在使用Rotativa將剃刀視圖變成PDF。無法在MVC4中使用Rotativa下載PDF文件

PDF文件未下載。

我可以在Fiddler中看到它,但瀏覽器不提示下載 - 我已經在IE和Chrome上試過了。

我也嘗試使用this question here.中的解決方案將文件下載到物理路徑,但由於系統無法訪問文件夾(拒絕訪問),該文件無法工作。

這裏是我的代碼:

public ActionResult Index() 
{ 
    var model = new CustomerDashboardVM(); 
    return View("Index", model); 
} 

public ActionResult print(int voucherID) 
{ 
    var pdf = new ActionAsPdf("Index", new { voucherID}) { FileName = "testInvoice.pdf", PageSize = Rotativa.Options.Size.A4}; 

    // RotativaHelper.SaveHttpResponseAsFile("http://localhost:65425/BlankDashboard", Server.MapPath("~\\PdfDownloads")); 

    return pdf; 

} 

我不知道爲什麼會這樣 - 我按一下按鈕,它會調用打印ActionResult的方法 - 沒有錯誤信息(我的一個嘗試,catch塊包裹着這一點)。我在同事的個人電腦上試過這個,這是同樣的問題!

非常感謝。

+0

而是** ActionAsPdf的**,你可以使用** ViewAsPdf **。所以你的代碼看起來像這樣 - 返回新的ViewAsPdf(「Index.cshtml」,new {voucherID}) { FileName =「testInvoice.pdf」, CustomSwitches =「--print-media-type --header-center \ 「text \」「 } ;.對格式問題感到抱歉。 –

+0

@KrishnrajRana謝謝隊友。這沒有用;它詢問了視圖的視圖模型(Index.cshtml)。我真的希望它顯示一個行動的結果,所以我想知道爲什麼這不會與ActionAsPdf工作 - 它適用於其他項目! –

+0

好的,告訴我你的** CustomerDashboardVM()**返回什麼?它是否返回通用列表?我在說這行 - ** var model = new CustomerDashboardVM(); **在Index()方法中 –

回答

1

問題是我在通過Ajax帖子在按鈕單擊事件上調用ActionResult print()方法。這顯然不起作用。

它應該工作,如果你用一個鏈接,其中的URL()mehtod的URL替換按鈕;也就是mvc鏈接的工作方式。

1

好的,我弄錯了。您在索引方法中沒有通過參數名稱voucherID

所以,現在你的代碼是這樣的:

public ActionResult Index(int voucherID) // Here you have to pass parameter 
{ 
    var model = new CustomerDashboardVM(voucherID); 
    return View("Index", model); 
} 

打印()方法是這樣的 -

public ActionResult Print(int voucherID) 
{ 
    return new ActionAsPdf(
       "Index", 
       new { voucherID = voucherID }) 
       { 
        FileName = "testInvoice.pdf", 
        PageSize = Rotativa.Options.Size.A4 
       }; 
}