MVC4 C#如何实现一键打印的当前信息 [英] MVC4 C# How to implement a button to print the current information

查看:409
本文介绍了MVC4 C#如何实现一键打印的当前信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来编程,我有一个按钮,当我点击它,它返回到previous页面。而不是按钮返回到previous页如何有一个按钮打印当前的信息?

下面是我的PrintIndexViewModel类

 使用NavisionStore.Domain.Entities;命名空间NavisionStore.WebUI.Models
{
    公共类PrintIndexViewModel
    {
        公共打印打印{搞定;组; }
        公共字符串RETURNURL {搞定;组; }
    }
}

这是我的打印控制器

 命名空间NavisionStore.WebUI.Controllers
{
    公共类PrintController:控制器
    {
        私人IProductRepository库;        公共PrintController(IProductRepository回购)
        {
            库=回购;
        }        公众的ViewResult指数(字符串RETURNURL)
        {
            返回查看(新PrintIndexViewModel
            {
                打印= GetPrint()
                RETURNURL = RETURNURL
            });
        }
         公共RedirectToRouteResult AddtoPrint(INT ID,字符串RETURNURL)
        {
            产品产品= repository.Products
                .FirstOrDefault(p值=> p.Id == ID);            如果(产品!= NULL)
            {
                。GetPrint()的AddItem(产品,1);
            }
            返回RedirectToAction(指数,新的{RETURNURL});
        }        公共RedirectToRouteResult RemoveFromPrint(INT ID,字符串RETURNURL)
        {
            产品产品= repository.Products
                .FirstOrDefault(p值=> p.Id == ID);            如果(产品!= NULL)
            {
                GetPrint()RemoveLine(产品)。
            }
            返回RedirectToAction(指数,新的{RETURNURL});
        }        私人打印GetPrint()
        {
            打印打印=(打印)会议[打印];
            如果(打印== NULL)
            {
                打印=新打印();
                会话[打印] =打印;
            }
            返回打印;
        }
    }
}

在这里查看printindexveiwmodel

  @model NavisionStore.WebUI.Models.PrintIndexViewModel@ {
    ViewBag.Title =标签袋:您打印;
}< H2> Caplugs西< / H>
<表格的宽度=90%对齐=中心>
    &所述; THEAD>&下; TR>
        百分位ALIGN =中心> PN:LT; /第i
        百分位ALIGN =中心>数量:LT; /第i
        百分位ALIGN =中心> B#< /第i
           < / TR>< / THEAD>
    <&TBODY GT;
        @foreach(在Model.Print.Lines VAR线){
            &所述; TR>
                < TD ALIGN =中心> @ line.Product.PartName< / TD>
                < TD ALIGN =中心> @ line.Product.Quantity< / TD>
                < TD ALIGN =中心> @ line.Product.BagNumber< / TD>
            < / TR>
        }
    < / TBODY>
    < /表>
    < p ALIGN =中心类=actionButtons>
        < A HREF =@ Model.ReturnUrl>打印和LT; / A>
    &所述; / P>


解决方案

您可能会考虑他们与你需要打印显示的数据发送到另一页(新标签?),然后使用用户点击,当打印样式表打印在浏览器上。你可以在你的样式表组语句仅用于打印:

  @media打印{
   。广告{
      显示:无;
   }
}

我不知道如果这是你在找什么。如果您希望能够强制打印对话框打开,我相信你会需要的JavaScript。您可以创建一个按钮,将打开他们的打印对话,他们(当前页面,用你的打印样式表来隐藏一切你不想打印):

 <输入类型=按钮值=打印此页的onclick =window.print()>

另一种方法是从你的控制器或诸如此类的东西返回一个PDF文件。

I am new to programming and I have a button that when I click on it, it returns to the previous page. instead of the button returning to previous page how to have that button print the current information?

Here is my PrintIndexViewModel Class

using NavisionStore.Domain.Entities;

namespace NavisionStore.WebUI.Models
{
    public class PrintIndexViewModel
    {
        public Print Print { get; set; }
        public string ReturnUrl { get; set; }
    }
}

here is my print controller

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;
        }
    }
}

here is view 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>

解决方案

You might consider sending them to another page (new tab?) with the data you need to print displayed, then use a print style sheet for when the user clicks print on the browser. You can group statements in your stylesheet to be used for printing only:

@media print{
   .ad{
      display: none;
   }
}

I am not sure if this is what you are looking for. If you want to be able to force the print dialogue to open, I believe you will need javascript for that. You can create a button that will open their print dialogue for them (of the current page and use your print style sheet to hide everything you don't want printed):

<input type="button" value="Print this page" onclick="window.print()">

Another option is to return a pdf from your controller or something of that sort.

这篇关于MVC4 C#如何实现一键打印的当前信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆