MVC4 C#如何实现按钮来打印当前信息

Mik*_*cko 2 c# asp.net-mvc-4

我是编程的新手,我有一个按钮,当我点击它时,它会返回上一页.而不是返回上一页的按钮如何让该按钮打印当前信息?

这是我的PrintIndexViewModel类

using NavisionStore.Domain.Entities;

namespace NavisionStore.WebUI.Models
{
    public class PrintIndexViewModel
    {
        public Print Print { get; set; }
        public string ReturnUrl { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的打印控制器

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是查看printindexveiwmodel

@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">Print</a>
    </p>
Run Code Online (Sandbox Code Playgroud)

Mon*_*per 6

您可以考虑将它们发送到另一个页面(新选项卡?),其中显示您需要打印的数据,然后使用打印样式表来确定用户在浏览器上单击打印的时间.您可以将样式表中的语句分组以仅用于打印:

@media print{
   .ad{
      display: none;
   }
}
Run Code Online (Sandbox Code Playgroud)

我不确定这是不是你想要的.如果你想强制打开打印对话框,我相信你需要javascript.您可以创建一个按钮,打开它们的打印对话框(当前页面并使用打印样式表隐藏您不想打印的所有内容):

<input type="button" value="Print this page" onclick="window.print()">
Run Code Online (Sandbox Code Playgroud)

另一个选择是从控制器或类似的东西返回一个pdf.