0

I am going to be really surprised if this is possible but here goes.

I am using the MVC WebGrid and have paging enabled to mange screen real estate better.

All was going well until the end user asked when they print the page they want to see all of the data not just the paged data.

Is it possible to when a user prints the screen I return the entire amount of data vs just the paged result in the print window?

A real dirty hack that I don't really want to do would be to render out a standard table hide it with css and create a print css file that only displays this hidden table.

Does anyone have any nicer solutions?

Diver Dan
  • 9,953
  • 22
  • 95
  • 166

1 Answers1

1

You can't get the browser to print what isn't there, and the idea of displaying all the data only to hide most of it is, as you say, a "real dirty hack". The cleanest option would be to provide you own Print button that links to another action that returns a View containing the WebGrid with paging turned off.

Simplified stuff - here's the action that returns the Print view:

public ActionResult Print() {
    var db = new NorthwindEntities();
    var products = db.Products.ToList();
    return View(products);
}

And here's the View:

@model IEnumerable<MvcApplication1.Models.Product>
@{
    ViewBag.Title = "Print";
    var grid = new WebGrid(Model, canPage: false);
}

@grid.GetHtml()
<script>
    $(function() {
        window.print();
    });
</script>

You need to make sure that jQuery is referenced, and the JavaScript will result in the browser's Print dialog appearing, displaying all the data.

Mike Brind
  • 28,238
  • 6
  • 56
  • 88