ASP.NET?MVC打印表格并實(shí)現(xiàn)部分視圖表格打印
假設(shè)在一個(gè)頁(yè)面上有眾多內(nèi)容,而我們只想把該頁(yè)面上的表格內(nèi)容打印出來(lái),window.print()方法會(huì)把整個(gè)頁(yè)面的內(nèi)容打印出來(lái),如何做到只打印表格內(nèi)容呢?
既然window.print()只會(huì)打印整頁(yè)的內(nèi)容,何不把表格放在一個(gè)部分視圖中,在部分視圖中再調(diào)用window.print()方法。
Model很簡(jiǎn)單:
public class Student { public int Id { get; set; } public string Name { get; set; } public decimal Score { get; set; } }
Home控制器中有一個(gè)Action方法返回Student的集合到部分視圖:
public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult PrintStudent() { var result = new List<Student> { new Student(){Id = 1, Name = "darren", Score = 90.9M}, new Student(){Id = 2, Name = "smith", Score = 91.8M}, new Student(){Id = 3, Name = "kathy", Score = 98.6M} }; return PartialView(result); } }
在Home/PrintStudent.cshtml這個(gè)強(qiáng)類(lèi)型視圖中調(diào)用window.print()方法:
@model IEnumerable<MvcApplication1.Models.Student> <style type="text/css"> .c { width: 100%; border: 1px solid green; border-collapse: collapse; } .c td { padding: 2px; border: 1px solid green; } </style> <style> /* 打印的時(shí)候讓打印按鈕隱藏 */ @@media only print { a { display: none; } } </style> <a href="#" rel="external nofollow" rel="external nofollow" onclick="window.print();return false;">打印表格</a> <table class="c"> <thead> <tr> <th>編號(hào)</th> <th>姓名</th> <th>分?jǐn)?shù)</th> </tr> </thead> <tbody> @foreach (var student in Model) { <tr> <td>@student.Id</td> <td>@student.Name</td> <td>@student.Score</td> </tr> } </tbody> </table> <a href="#" rel="external nofollow" rel="external nofollow" onclick="window.print();return false;">打印表格</a>
在Home/Index.cshtml視圖中,點(diǎn)擊按鈕,彈出部分視圖內(nèi)容:
@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <button id="p">打印已經(jīng)確定好的內(nèi)容</button> @section scripts { <script type="text/javascript"> $(function() { $('#p').click(function() { $.ajax({ url: '@Url.Action("PrintStudent","Home")', success: function(data) { if (judgePopupBlocked) { alert("瀏覽器禁用彈出窗口了,請(qǐng)?jiān)试S彈出窗口"); } var popUpWindow = window.open(); if (popUpWindow) { $(popUpWindow.document.body).html(data); } else { alert("瀏覽器禁用彈出窗口了,請(qǐng)?jiān)试S彈出窗口"); } } }); }); }); //判斷瀏覽器是否阻止了彈出窗口 function judgePopupBlocked() { var w = window.open(null, "", "width=1,height=1"); try { w.close(); return false; } catch (e) { return true; } } </script> }
點(diǎn)擊"打印已經(jīng)確定好的內(nèi)容"按鈕:
取消禁用彈出窗口,再次點(diǎn)擊"打印已經(jīng)確定好的內(nèi)容"按鈕:
點(diǎn)擊"打印表格":
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
一個(gè)比較通用的分頁(yè)控件,完整的設(shè)計(jì)時(shí)支持和比較流行的分頁(yè)模式(提供源碼下載)
本分頁(yè)控件還包含簡(jiǎn)單屬性,復(fù)雜屬性,自定義視圖狀態(tài),分頁(yè)事件,創(chuàng)建控件,render控件,Attribute,設(shè)計(jì)時(shí)支持等比較齊全的自定義控件的元素,是個(gè)不錯(cuò)學(xué)習(xí)自定義控件開(kāi)發(fā)的例子2010-12-12Asp.net中使用Sqlite數(shù)據(jù)庫(kù)的方法
Sqlite是最近比較流行的數(shù)據(jù)庫(kù)了,擁有比Access高效快速,易操作易實(shí)施。完全不需要在客戶端進(jìn)行任何的配置,只需要在站點(diǎn)中引用入DLL文件即可使用了。2009-11-11Entity?Framework使用Fluent?API配置案例
本文詳細(xì)講解了Entity?Framework使用Fluent?API配置案例的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03ASP.NET頁(yè)面之間傳值的方式之Application實(shí)例詳解
這篇文章主要介紹了ASP.NET頁(yè)面之間傳值的方式之Application實(shí)例詳解,需要的朋友可以參考下2017-10-10.NET與樹(shù)莓派控制彩色燈帶WS28XX的實(shí)現(xiàn)
這篇文章主要為大家介紹了.NET與樹(shù)莓派控制彩色燈帶WS28XX的實(shí)現(xiàn)過(guò)程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04在Framework4.0中實(shí)現(xiàn)延遲加載的實(shí)現(xiàn)方法
延遲加載,亦稱(chēng)延遲實(shí)例化,延遲初始化等,主要表達(dá)的思想是,把對(duì)象的創(chuàng)建將會(huì)延遲到使用時(shí)創(chuàng)建,而不是在對(duì)象實(shí)例化時(shí)創(chuàng)建對(duì)象,即用時(shí)才加載。2011-08-08