首先你的内容一定要放在一个div中如下代码

   <div id="divprint">
<table class="table table-striped dataTable table-bordered" id="DataTables_Table_0" aria-describedby="DataTables_Table_0_info">
<thead>
<tr role="row">
<th class="sorting_asc" role="columnheader" tabindex="" aria-controls="DataTables_Table_0" rowspan="" colspan="" aria-sort="ascending" aria-label="Rendering engine: activate to sort column descending" style="width: 140px;">用户名ID</th>
<th class="sorting" role="columnheader" tabindex="" aria-controls="DataTables_Table_0" rowspan="" colspan="" aria-label="Browser: activate to sort column ascending" style="width: 180px;">用户名</th>
<th class="sorting" role="columnheader" tabindex="" aria-controls="DataTables_Table_0" rowspan="" colspan="" aria-label="Platform(s): activate to sort column ascending" style="width: 160px;">电话</th>
<th class="sorting" role="columnheader" tabindex="" aria-controls="DataTables_Table_0" rowspan="" colspan="" aria-label="Engine version: activate to sort column ascending" style="width: 140px;">真实姓名</th>
<th class="sorting" role="columnheader" tabindex="" aria-controls="DataTables_Table_0" rowspan="" colspan="" aria-label="CSS grade: activate to sort column ascending" style="width: 140px;">工号</th>
<th class="sorting" role="columnheader" tabindex="" aria-controls="DataTables_Table_0" rowspan="" colspan="" aria-label="CSS grade: activate to sort column ascending" style="width: 167px;">拥有角色</th>
</tr>
</thead> <tbody role="alert" aria-live="polite" aria-relevant="all">
@foreach (var item in Model.EntityList)
{
<tr>
<td class=" sorting_1">@item.OneUser.Uid</td>
<td class=" ">@item.OneUser.UserName</td>
<td class=" ">@item.OneUser.Tel</td>
<td class=" ">@item.OneUser.TrueName</td>
<td class=" ">@item.OneUser.WorkNo</td>
<td class=" ">
@if (item.HasRole!=null&&item.HasRole.Count>)
{
foreach (var name in item.HasRole)
{
<p>@name</p>
}
}
</td>
</tr>
} <tr class="odd">
<td class=" sorting_1">Gecko</td>
<td class=" ">Firefox 1.0</td>
<td class=" ">Win + / OSX.+</td>
<td class=" ">1.7</td>
<td class=" ">A</td>
</tr>
<tr class="even">
<td class=" sorting_1">Gecko</td>
<td class=" ">Firefox 1.5</td>
<td class=" ">Win + / OSX.+</td>
<td class=" ">1.8</td>
<td class=" ">A</td>
</tr>
</tbody>
</table>
</div>
<input type="button" id="btndy" value="打印" />

然后我们看JS代码部分:(注意:因为这里部分打印损坏了HTML代码结构,所以不管你打不打印都要重新加载当前页面,这里我们用window.location.href="/Account/UserManagerMent";或window.reload();)

        $(function () {
$("#btndy").click(function () {
var headstr = "<html><head><title></title></head><body>";
var footstr = "</body>";
var newstr = $("#divprint").html(); // 获取要打印的div内容
var oldstr = document.body.innerHTML; // 将过去的body内容存到oldstr
document.body.innerHTML = headstr + newstr + footstr; // 组合打印内容
window.print(); // 弹出打印窗体
document.body.innerHTML = oldstr; // 还原HTML内容
window.location.href="/Account/UserManagerMent"; // 刷新当前页面
});
});

至于后台以前是什么样,现在还是什么样,只是刷新一下而已

小Y子的代码和我的差不多

<button onclick="doPrint()">打印</button>
<div id="pri">
111111111
</div>
<script>
function doPrint(){
var head_str = "<html><head><title></title></head><body>"; //先生成头部
var foot_str = "</body></html>"; //生成尾部
var older = document.body.innerHTML;
var new_str = document.getElementById('pri').innerHTML; //获取指定打印区域
var old_str = document.body.innerHTML; //获得原本页面的代码
document.body.innerHTML = head_str + new_str + foot_str; //构建新网页
window.print(); //打印刚才新建的网页
document.body.innerHTML = older; //将网页还原
return false;
}
</script>

  

关于MVC打印问题,打印指定的内容的更多相关文章

  1. 在ASP.NET2.0里打印网页指定的内容(比如打印网页里的一个Table)

    原文:在ASP.NET2.0里打印网页指定的内容(比如打印网页里的一个Table) 打印指定内容: <html> <head> <script   type= " ...

  2. HOW TO:使用 Visual C# .NET 打印 RichTextBox 控件的内容

    概要 本分步指南介绍了如何打印 RichTextBox 控件的内容.RichTextBox 控件没有提供任何打印 RichTextBox 内容的方法.您可以扩展 RichTextBox 类以使用 EM ...

  3. C#如何打印RichTextBox控件的内容

      本任务的内容 摘要 创建 RichTextBoxPrintCtrl 控件 测试控件 参考 概要 本分步指南介绍了如何打印 RichTextBox 控件的内容.RichTextBox 控件没有提供任 ...

  4. python 打印 str 字符串的实际内容 repr(str)

    python 打印 str 字符串的实际内容 repr(str) s = 'aa' print(repr(s))

  5. C#实现打印与打印预览功能

    C#实现打印与打印预览功能的思路及代码. 在windows应用程序中文档的打印是一项非常重要的功能,在以前一直是一个非常复杂的工作,Microsoft .Net Framework的打印功能都以组件的 ...

  6. C#教程之打印和打印预览

    最近研究一了一下关于PDF打印和打印预览的功能,在此小小的总结记录一下学习过程. 实现打印和打印预览的方法,一般要实现如下的菜单项:打印.打印预览.页面设置. PrintDocument类 Print ...

  7. C#实现打印与打印预览功能(转)

    在windows应用程序中文档的打印是一项非常重要的功能,在以前一直是一个非常复杂的工作,Microsoft .Net Framework的打印功能都以组件的方式提供,为程序员提供了很大的方便,但是这 ...

  8. python3获取指定目录内容的详细信息

    不同平台获取指定目录内容的详细信息命令各不相同: Linux中可以通过ls -al获取获取 windows中可以通过dir命令获取 下面是我写的一个通用获取目录内容详细信息的python3脚本: #! ...

  9. 获取txt文件指定行内容

    #!/usr/bin/python num=0; ni=open("C:\Python34\ceshi.txt") for line in ni: num=num+1;  #表示行 ...

  10. android网页打印,安卓网页打印,h5页面打印,浏览器打印,js打印工具

    Android设备打印比较麻烦,一般设备厂商都提供原生app开发的SDK,我们web开发者为难了,不会原生开发啊 给大家提供一个思路,实现web加壳,利用打印浏览器实现 简单来说就是把我们的web页面 ...

随机推荐

  1. std::thread(2)

    个线程都有一个唯一的 ID 以识别不同的线程,std:thread 类有一个 get_id() 方法返回对应线程的唯一编号,你可以通过 std::this_thread 来访问当前线程实例,下面的例子 ...

  2. Hadoop单点伪分布模式安装

    Hadoop单点伪分布模式安装 概述 单点 single-node,单节点,即一台计算机. 伪分布式模式 pseudo-distributed mode 所谓集群,表面上看是多台计算机联合完成任务:但 ...

  3. SaltStack数据系统-Grains

    上一篇:SaltStack配置管理 granis:谷粒 pillar:柱子 grains是salt的一个组件,存放minion启动时候收集的信息(状态信息) 查看 salt '*' grains.it ...

  4. Oracle涂抹oracle学习笔记第9章RMAN说,我能恢复

    RMAN中的恢复对应两个操作:数据库修复(restore)和数据库恢复(recover) 数据库修复(restore):是指利用备份集的数据文件来替换已经损坏的数据库文件或者将其恢复到一个新的位置.R ...

  5. react-native 学习(三)

    上一次讲到了react-native 的配置环境 和 如何去进行调试,这一次我们说一说,关于react-native的 样式兼容问题. 由于iphonex的发售,在兼容的时候,我门也需要去考虑ipho ...

  6. 修改记事本默认编码为UTF-8

    1. 新建一个txt文档,不输入任何内容.然后“另存为”,将编码由默认的 ANSI 修改为 Unicode 或 UTF-8,并将新文档命名为 temp.txt 2.将 temp.txt 移动至系统目录 ...

  7. Browser Cookie Limits

    w https://cait.calarts.edu/hc/en-us/articles/217055138-Error-Maximum-Number-of-Cookie-Values-Reached ...

  8. ArcGIS Server管理工具之批量发布动态地图服务工具.md

    友好阅读链接:(http://swj.me/2015/08/26/batchPublishtools/) update0918: 修复了创建链接文件时而出错的bug 修复了在24011的警告已处理的情 ...

  9. MySQL优化(三):优化数据库对象

    二.优化数据库对象 1.优化表的数据类型 应用设计的时候需要考虑字段的长度留有一定的冗余,但不推荐很多字段都留有大量的冗余,这样既浪费磁盘空间,也在应用操作时浪费物理内存. 在MySQL中,可以使用函 ...

  10. 关闭linux的防火墙

    有两道防火墙 第一道 iptables -L iptables -F systemctl disable firewalld 第二道 [root@python3 ~]# getenforce Enfo ...