Reputation: 55
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>
Upvotes: 3
Views: 10506
Reputation: 1579
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.
Upvotes: 7