We help IT Professionals succeed at work.

Using partial views in Razor

How do I load a partial view from a cshtml file?

I thought I could use:
@Html.RenderPartial("razorFilename")

But it doees not yet work. Is this correct and pointing to an error in the format of my partial View?  What are the rules of a partial page's format?
Comment
Watch Question

Hi!
Try @Html.Partial("razorFilename")
I guess rules for partial views are the same as for layouts and other page. Partial views are just substituted in page and rendered as other parts of the page.
curiouswebsterSoftware Engineer

Author

Commented:
> I guess rules for partial views are the same as for layouts and other page

Well, there are certain sections a normal page must have that a partial can not have. I was hoping for a list.
What sections do you mean? As for me, I did not faced any restriction on partial view - they can have their own layouts and use child partial views and sections like other pages.
curiouswebsterSoftware Engineer

Author

Commented:
I thought a partial view did not have a body tag? Maybe I was wrong.
Patial view can contain any html tags, that will be rendered to the page.But some tags as <Body> or <Head> are rarely used in partial views, as they are usually put in Layout (but it does not mean that partial view can't have them)
curiouswebsterSoftware Engineer

Author

Commented:
I the _Layout.cshtml has a <Body> and a <Head> section, does that mean the partial page can not have one also? Or would the partial's take precedence?
curiouswebsterSoftware Engineer

Author

Commented:
"If the" is what I meant
All html  tags in partial view will be rendered as is. So if both Layout and Partial view has body tags, you can get following html:

<head> <!-- this is head from Layout-->
..
</head>

<body><!-- this is body from Layout-->
..
<head> <!-- this is head from partial view-->
...
</head>
<body> <!-- this is body from partial view-->
....
</bodY>
...
..
</body>

How to interpret such html (which is invalid due to specification)  is up to client browser. It looks like IE just ignore nested head and body.
curiouswebsterSoftware Engineer

Author

Commented:
Thakns!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.