ASP.NET?MVC打印表格并實(shí)現(xiàn)部分視圖表格打印
假設(shè)在一個(gè)頁(yè)面上有眾多內(nèi)容,而我們只想把該頁(yè)面上的表格內(nèi)容打印出來,window.print()方法會(huì)把整個(gè)頁(yè)面的內(nè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)類型視圖中調(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)文章
ASP.NET Core文件上傳與下載實(shí)例(多種上傳方式)
下面小編就為大家分享一篇ASP.NET Core文件上傳與下載實(shí)例(多種上傳方式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Grid或者DataTable中數(shù)據(jù)導(dǎo)出為Excel原來這么簡(jiǎn)單
以前一直認(rèn)為,將Grid 或者DataTable中的數(shù)據(jù)導(dǎo)出到Excel功能實(shí)現(xiàn)會(huì)非常復(fù)雜,可能會(huì)想用什么類庫(kù)什么的或者實(shí)在太難就用csv算了,沒想到真的很簡(jiǎn)單,需要了解的朋友可以參考下2012-12-12
ASP.NET WebAPI2復(fù)雜請(qǐng)求跨域設(shè)置的方法介紹
這篇文章主要給大家介紹了關(guān)于ASP.NET WebAPI2復(fù)雜請(qǐng)求跨域設(shè)置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用ASP.NET具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
asp.net System.Guid ToString五種格式
這篇文章主要介紹了asp.net System.Guid ToString五種格式,需要的朋友可以參考下2017-02-02
設(shè)置默認(rèn)Ajax操作cache and error
設(shè)置默認(rèn)Ajax操作cache and error,需要的朋友可以參考一下2013-02-02
ASP.NET MVC 從IHttp到頁(yè)面輸出的實(shí)例代碼
MVCHandler應(yīng)該算是MVC真正開始的地方。MVCHandler實(shí)現(xiàn)了IHttpHandler接口,ProcessRequest便是方法入口2013-09-09
在Asp.net core項(xiàng)目中使用WebSocket
這篇文章介紹了在Asp.net core項(xiàng)目中使用WebSocket的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
詳解ASP.NET MVC 解析模板生成靜態(tài)頁(yè)(RazorEngine)
我們?cè)诤芏囗?xiàng)目開發(fā)中會(huì)常常用到頁(yè)面靜態(tài)化,本篇文章主要介紹了詳解ASP.NET MVC 解析模板生成靜態(tài)頁(yè)(RazorEngine) ,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-03-03

