ASP.NET MVC > Partial view

Render/call a partial view from a content page (View) in ASP.NET MVC

How to render/call a partial view from a content page (View) ?


To render a partial view from the View page, we can either user @Html.Partial or @Html.RenderPartial helper method.

<hr />
@Html.Partial("_GetFileName")
<hr />
@{ Html.RenderPartial("_GetFileName"); }
<hr />

@Html.Partial returns MvcHtmlString that is the output of the Partial view. However @Html.RenderPartial view has been called in the code block (wrapped with @{ and } as @Html.RenderPartial method returns void and its result is written in the Response stream.

POINT OF INTEREST

Here, we are assuming that _GetFileName.cshtml partial view exists in the same folder where the calling view exists. If not, ASP.NET MVC framework search for /Views/Shared folder for this view, if the view is not found there then it throws error.


 Views: 36693 | Post Order: 51



Write for us






Hosting Recommendations