Nested partial view in ASP.NET MVC

How to call a partial view from another partial view in ASP.NET MVC?

To call a partial view from another partial view, we follow the same approach as we follow when we call partial view from the content page.
Let's take an example. 
Partial view 1
This is the code of 1st partial view.
View 1
<hr />
@Html.Partial("_View2")
Notice that in above partial view, we are calling another partial view named "_View2" that is created below.
Partial view 2
This is the code of 2nd partial view.
View 2

Calling partial view

To call the 1st partial view into the content page, write below code
@Html.Partial("_View1")

<hr /><hr />
@{
    Html.RenderPartial("_View1"); // another way to call partial view   
}
OUTPUT

The 1st set of View1 and View2 text is from @Html.Partial and 2nd set is from @Html.RenderPartial method.

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