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.RenderPartialhelper 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.

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...