Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 796
Andreas Eriksson Avatar asked May 20 '11 06:05

Andreas Eriksson


People also ask

Can we create view without model in MVC?

If you don't want to create View Model then it is completely fine. You can loose some benefit like automatic client side validation ( data annotation). It means you have to do all validation by your self in client side and later on server side. This is simple way you can do that.

What is _layout Cshtml in MVC?

The file "_Layout. cshtml" represents the layout of each page in the application. Right-click on the Shared folder in Solution Explorer then go to "Add" item and click on "View". Now the View has been created.

Can we use master page in MVC?

You add a new view master page to an MVC project by right-clicking the Views\Shared folder, selecting the menu option Add, New Item, and selecting the MVC View Master Page template (see Figure 1). You can create more than one view master page in an application. Each view master page can define a different page layout.


2 Answers

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.

like image 121
archil Avatar answered Sep 20 '22 05:09

archil


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> 
like image 44
Nagaraj Raveendran Avatar answered Sep 23 '22 05:09

Nagaraj Raveendran