RenderPage And Data in ASP.NET MVC 3 Web Pages
Posted by: Malcolm Sheridan
in Category ASP.NET MVC
Abstract: The following article demonstrates how to pass data into the RenderPage method in ASP.NET MVC 3 Web Pages.
When you’re working with MVC, you sometimes work with partial views. With the new Razor view engine, partial views can be called through the RenderPage method. RenderPage renders the content of one page within another page. What isn’t obvious from the beginning is how to access data that is passed into the partial page. I thought I’d do a quick article and show you how. This code also works in WebMatrix if you're using it. If you want to know more about RenderPage, read my previous article ASP.NET MVC 3 Razor Syntax - RenderBody, RenderPage and RenderSection
Before moving on, you need to download ASP.NET MVC 3. Click here to download and install it using the Microsoft Web Platform Installer.
Open studio 2010 and create a new ASP.NET MVC 3 Web Application (Razor) project. For this example, my model will be a list of processes running on your machine. I want to pass that model into the view, and then pass that model into the partial page. When you’re working with partial views, you need to specify a path to the view, then an optional array of data to the page being rendered. The array of data can be accessed by using the System.Web.WebPages.WebPageBase.PageData property. Here’s the model for the view.
And here’s the view.
I’m going to create a partial view called _DisplayProcess and call it via the RenderPage method. When I create partial views I always prefix them with an underscore so they can only be rendered as a partial view. By default ASP.NET won’t render files beginning with an underscore. The second parameter for RenderPage is the array of data. To use this, you can use the PageData property.
If you only had a single object being passed into the page, you would reference it like this.
To me it wasn’t obviously the first time I used partial views in Razor, so hopefully if you get stuck like me, this helps you out.
The entire source code of this article can be downloaded over here
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!