Displaying a page in MVC 3 without layout

C#.Netasp.net Mvc-3

C# Problem Overview


I have a page that generates a printable table. I need to show this page without my surrounding _Layout page, for printer-friendliness.

How would I go about doing this?

C# Solutions


Solution 1 - C#

Assuming you use razor view engine (you mentioned layout, not master page)

@{
    Layout = null;
 }

Well actually you should use razor view engine but anyways, idea is simple. Do not specify (remove) master page file reference in your aspx view and remove all ContentPlaceHolders, write all content directly in page. Or there's another way if you don't wish to remove them for some reason. Make PrintMaster.master master page which will contain nothing but ContentPlaceHolders.

Solution 2 - C#

While creating a new view, you can uncheck the use layout checkbox. 
This will create you a view with layout as null.

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Test</title>
</head>
<body>
    <div> 
    </div>
</body>
</html>

Solution 3 - C#

When you create the view it allows you to change the Master Page. If you unmark the checkbox, the view comes with no Master Page and you can modify the whole page.

Solution 4 - C#

If you need to support displaying results on a page as well as having a printable view, you could create a second view (named PrintView for example) that does not use a page layout and call return View("PrintView"); from your controller.

Solution 5 - C#

A standard print style action can be done in several ways.

  1. use a different view with a print button that sets the layout to null assuming you can map to razor.

To do this with CSS - you will want a separate css file that will be loaded on print and will hide your masterpage items. See the various articles on keywords css media print for example: http://webdesign.about.com/cs/css/a/aa042103a.htm

This uses

<link rel="stylesheet" type="text/css" href="print.css" media="print" />

with the key here being media="print" which will use that css during print only.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndreas ErikssonView Question on Stackoverflow
Solution 1 - C#archilView Answer on Stackoverflow
Solution 2 - C#Nagaraj RaveendranView Answer on Stackoverflow
Solution 3 - C#Carles CompanyView Answer on Stackoverflow
Solution 4 - C#dariomView Answer on Stackoverflow
Solution 5 - C#Adam TuliperView Answer on Stackoverflow