Andreas Eriksson
Andreas Eriksson

Reputation: 9027

Displaying a page in MVC 3 without layout

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?

Upvotes: 56

Views: 51842

Answers (5)

Nagaraj Raveendran
Nagaraj Raveendran

Reputation: 1220

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>

Upvotes: 4

archil
archil

Reputation: 39501

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.

Upvotes: 125

Adam Tuliper
Adam Tuliper

Reputation: 30152

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.

Upvotes: 0

Carles Company
Carles Company

Reputation: 7216

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.

Upvotes: 2

dariom
dariom

Reputation: 4578

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.

Upvotes: 1

Related Questions