C# 如何打印项目和打印网页中的某些项目

C# 如何打印项目和打印网页中的某些项目,c#,asp.net,C#,Asp.net,我想做的是能够打印出一个网页。我相信我正在寻找一个打印预览页,但我不知道如何做到这一点。我只想打印出当前页面的某些信息 这是我的控制器 using NavisionStore.Domain.Abstract; using NavisionStore.Domain.Entities; using NavisionStore.WebUI.Models; using System; using System.Collections.Generic; using System.Linq; using S

我想做的是能够打印出一个网页。我相信我正在寻找一个打印预览页,但我不知道如何做到这一点。我只想打印出当前页面的某些信息

这是我的控制器

using NavisionStore.Domain.Abstract;
using NavisionStore.Domain.Entities;
using NavisionStore.WebUI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NavisionStore.WebUI.Controllers
{
    public class PrintController : Controller
    {
        private IProductRepository repository;

        public PrintController(IProductRepository repo)
        {
            repository = repo;
        }

        public ViewResult Index(string returnUrl)
        {
            return View(new PrintIndexViewModel
            {
                Print = GetPrint(),
                ReturnUrl = returnUrl
            });
        }
         public RedirectToRouteResult AddtoPrint(int id, string returnUrl)
        {
            Product product = repository.Products
                .FirstOrDefault(p => p.Id == id);

            if (product != null)
            {
                GetPrint().AddItem(product, 1);
            }
            return RedirectToAction("Index", new { returnUrl });
        }

        public RedirectToRouteResult RemoveFromPrint(int id, string returnUrl)
        {
            Product product = repository.Products
                .FirstOrDefault(p => p.Id == id);

            if (product != null)
            {
                GetPrint().RemoveLine(product);
            }
            return RedirectToAction("Index", new { returnUrl });
        }

        private Print GetPrint()
        {
            Print print = (Print)Session["Print"];
            if (print == null)
            {
                print = new Print();
                Session["Print"] = print;
            }
            return print;
        }
    }
}
这是我的视图模型类

using NavisionStore.Domain.Entities;

namespace NavisionStore.WebUI.Models
{
    public class PrintIndexViewModel
    {
        public Print Print { get; set; }
        public string ReturnUrl { get; set; }
    }
}
这是我的索引视图模型

@model NavisionStore.WebUI.Models.PrintIndexViewModel

@{
    ViewBag.Title = "Bag Labels: Your Print";
}

<h2>Caplugs West</h2>
<table width="90%" align="center">
    <thead><tr>
        <th align="center">PN:</th>
        <th align="center">QTY:</th>
        <th align="center">B#</th>
           </tr></thead>
    <tbody>
        @foreach(var line in Model.Print.Lines) {
            <tr>
                <td align="center">@line.Product.PartName</td>
                <td align="center">@line.Product.Quantity</td>
                <td align="center">@line.Product.BagNumber</td>
            </tr>
        }
    </tbody>
    </table>
    <p align="center" class="actionButtons">
        <a href="@Model.ReturnUrl">Continue shopping</a>
    </p>

您可以使用CSS来控制打印页面时发生的事情

例如,在您的css文件中,如果您这样做

@media print {
  .noprint { display: none; }
}
那你就可以了

@model NavisionStore.WebUI.Models.PrintIndexViewModel

@{
    ViewBag.Title = "Bag Labels: Your Print";
}

<h2>Caplugs West</h2>
<table width="90%" align="center">
<thead><tr>
    <th align="center">PN:</th>
    <th align="center">QTY:</th>
    <th align="center">B#</th>
       </tr></thead>
<tbody>
    @foreach(var line in Model.Print.Lines) {
        <tr>
            <td align="center">@line.Product.PartName</td>
            <td align="center">@line.Product.Quantity</td>
            <td align="center">@line.Product.BagNumber</td>
        </tr>
    }
</tbody>
</table>
<p align="center" class="actionButtons noprint">
    <a href="@Model.ReturnUrl">Continue shopping</a>
</p>
我还创建了一个JS小提琴来显示它的工作

现在,当您查看页面时,您将获得完整的页面,但您的打印(或浏览器打印预览对话框)将仅包含内容


您还可以在
@media print
部分的
正文
html
中添加一些格式,以进一步格式化页面布局(例如,设置宽度)

当我执行此操作时,我可以打印页面,但我只想打印页面的某些部分,而不是整个页面。由于我没有10级声誉,我无法添加网页截图,以便更好地解释我自己。我想我需要添加一个打印预览屏幕,以便我只能从网页中获取某些信息,然后打印打印预览页面?@MikeMacko此css将允许打印版本的页面排除某些数据。因此,任何标记为
noprint
类的元素都将出现在屏幕上,但在打印时将被排除在外it@MikeMacko真的需要一个屏幕截图,你不能提供pastebin链接吗?我在pastebin上看不到附加文件或显示图片的选项我不确定如何使用pastbaord我单击并将我的屏幕快照拖到它上,然后如何上载照片没有选项
@media print {
  .noprint { display: none; }
}
@model NavisionStore.WebUI.Models.PrintIndexViewModel

@{
    ViewBag.Title = "Bag Labels: Your Print";
}

<h2>Caplugs West</h2>
<table width="90%" align="center">
<thead><tr>
    <th align="center">PN:</th>
    <th align="center">QTY:</th>
    <th align="center">B#</th>
       </tr></thead>
<tbody>
    @foreach(var line in Model.Print.Lines) {
        <tr>
            <td align="center">@line.Product.PartName</td>
            <td align="center">@line.Product.Quantity</td>
            <td align="center">@line.Product.BagNumber</td>
        </tr>
    }
</tbody>
</table>
<p align="center" class="actionButtons noprint">
    <a href="@Model.ReturnUrl">Continue shopping</a>
</p>
@media print {
  #header, #categories, .actionButtons {
      display: none;
  }

  DIV#content {
      border-left: 0;
      margin-left: 0;
  }
}