Hanno Opperman
Hanno Opperman

Reputation: 143

How to create a customized print function for a view in ASP.NET MVC

I have a C# ASP.NET MVC and I'm stuck on one piece of functionality. My project is a game management system for a hobby.

After the user add a certain number of players and creates a game, it returns a view with all the games. (Two teams per game).

Each game and team are generated by two foreach loops, the first one loops through each of the games and displays them and the second one inside of it loops through the teams to display the players.

I want to add a button that says something along the lines of "Print" which will let the users download a pdf, but there needs to be only one game per page. (It's an hobby for old people, they have bad eyes)

All suggestions are welcome, I'm not exactly sure where to start.

Upvotes: 1

Views: 1402

Answers (1)

Stinky Towel
Stinky Towel

Reputation: 778

You have options.

  1. Depending on how you're marking up the display of the games, teams and players data, you could use a CSS file for printing. <link href="printme.css" rel="stylesheet" type="text/css" media="print" /> Display a <table> for each game and its related data, then play with default height of the selector such that it displays only one per page when printing. The tradeoff with this is you may have to disable other tags/selectors depending on what you want to show/hide and if some game tables have a lot of data, then you could get unexpected results in the printted display.

  2. Use an SSRS RDLC report which gives you total control over the printted display. Using MVC you allow your end users to download the PDF. This is not hard, but it is involved. Assuming you can create an RDLC file in your version of VS, here's a very high level of what you'll need to do:

    • Create data transfer objects (DTOs) that hold game, team and player info. Your Game DTO may have a Team DTO type which has a List of Player types. Make sure your DTOs have the longform getter/setters. This is required to make the RDLC side happy. For example,

    private long _gameID; public long GameID { get { return _gameID; } set { _gameID = value; } }

    • Create a dataset (XSD) with datatables who's field names match your DTOs. So you should have a Game, Team and Player datatables. You'll need to explicitly link these in the dataset designer. These links will denote the master detail replationship in the RDLC report.
    • Add an RDLC file to your project and use the VS editor to create the layout you want. Add your dataset/datatables as a datasource. Then add the controls you need to display the data. You can use the Tablix control or create subreports to accomplish the master/detail display (i.e. each Game with Teams with Players).
    • From there you can config the RDLC to only display one Game per page.
    • Add logic to link the dataset to the RDLC, then create the report and set its output as PDF.
    • Create an MVC controller that calls the RDLC code that creates the report and returns FileContentResult

Again the above is high level, but there are good resources on the web that can help you through it. Here's a good article to get you going

Upvotes: 1

Related Questions