首先你的内容一定要放在一个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. shell命令发送网站请求

    GET请求:curl "http://192.168.87.195:8888/refresh" POST请求:curl -d "name=value" &quo ...

  2. 160412、DWR使用笔记

    DWR是一个框架,简单的说就是能够在javascript直接调用java方法,而不必去写一大堆的javascript代码.它的实现是基于ajax的,可以实现无刷新效果. 网上有不少DWR的例子,但大都 ...

  3. Spoken English Practice(And I can't walk. Should i just stay home and pout about it?)

    绿色:连读:                  红色:略读:               蓝色:浊化:               橙色:弱读     下划线_为浊化 口语蜕变(2017/7/12) ...

  4. netty 网关 flume 提交数据 去除透明 批处理 批提交 cat head tail 结合 管道显示行号

    D:\javaNettyAction\NettyA\src\main\java\com\test\HexDumpProxy.java package com.test; import io.netty ...

  5. If the parts of an organization (e.g., teams, departments, or subdivisions) do not closely reflect the essential parts of the product, or if the relationship between organizations do not reflect the r

    https://en.wikipedia.org/wiki/Conway%27s_law

  6. Robberies---hdu2955(概率dp,01背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955 题目给了每个银行的钱和被抓的概率,由于要抢尽量多的钱,所以要保证尽量不被抓,而抢多个银行之后不被 ...

  7. golang zlib 压缩,解压缩

    package main import ( "bytes" "compress/zlib" "fmt" "io" &qu ...

  8. 使用Custom Draw优雅的实现ListCtrl的重绘

    common control 4.7版本介绍了一个新的特性叫做Custom Draw,这个名字显得模糊不清,让人有点摸不着头脑,而且MSDN里也只给出了一些如风的解释和例子,没有谁告诉你你想知道的,和 ...

  9. 浅析Android View(二)

    深入理解Android View(一) View的位置參数信息 二.View的绘制过程 View的绘制过程一共分为三个部分: - measure(測量View的大小) - layout(确定View的 ...

  10. django views.py视图 获取用户请求相关信息以及请求头

    请求的其他信息 用户发来请求时候,不仅发来数据,也把请求头也发过来 在views.py 怎么找请求数据? request是一个对象,这个对象封装很多信息,可以先查这个对象的类 print(type(r ...