Paria Shiri
Paria Shiri

Reputation: 97

Share a View between ComponentViews

I wonder if there is any way to share a View between ComponetViews. I like to share a Menu between some ComponetViews, I have tried to share it as PartalView but it seems it is not possible.

Upvotes: 0

Views: 131

Answers (1)

Jerry Cai
Jerry Cai

Reputation: 949

Partail view can be shared between View Components.

You can check this demo:

Two ViewComponents:

@model IEnumerable<ViewComponentSample.Models.TodoItem
<h3>First View Component</h3>   
@await Html.PartialAsync("/Views/Shared/_partial.cshtml")  //partial here

partial view:

@{
List<ViewCompFinal.Models.Menu> menulist = new List<ViewCompFinal.Models.Menu>
{
    new ViewCompFinal.Models.Menu{ Name="Beer"},
    new ViewCompFinal.Models.Menu{ Name="Beef"}
}; 
}

@foreach (var item in menulist)
{
     @item.Name<br/>
}

Result:

enter image description here

You can check how to use partial view and viewComponent here:

partial view

view component

Upvotes: 1

Related Questions