3

私はプログラミングが初めてで、クリックすると前のページに戻るボタンがあります。前のページに戻るボタンの代わりに、そのボタンに現在の情報を印刷させる方法は?

これが私のPrintIndexViewModelクラスです

using NavisionStore.Domain.Entities;

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

これが私のプリントコントローラーです

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

これがビュー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>
4

1 に答える 1

7

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.

于 2013-11-07T15:29:18.290 に答える