Share via

Partial Views question

Stesvis 1,041 Reputation points
2021-01-03T21:31:02.387+00:00

I am using some partial views in my app, just to keep code organized and also to reuse some views.
My partial views have a ViewModel as well, but I don't understand how to make data from the partial view available to the parent view.

For example:

ParentPage:

<ContentPage.Content>
    <views:PartialView1 />
    <views:PartialView2 />
</ContentPage.Content>

Can the ParentPage read data from PartialView1 or PartialView2?
Let's say that in PartialView1ViewModel I have a property public List<Post> Posts { get; set; } how can I read the value from ParentPageViewModel?

Developer technologies | .NET | Xamarin
0 comments No comments

1 answer

Sort by: Most helpful
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,756 Reputation points
    2021-01-04T02:03:15.057+00:00

    Hello,

    Welcome to Microsoft Q&A!

    The best way is to create a PartialView1ViewModel instance inside ParentPageViewModel , initial and populate data , then you can access the data inside ParentPageViewModel.

    For example

       public class PartialView1ViewModel  
           {  
               public List<Post> Posts { get; set; }  
           }  
    
           public class ParentPageViewModel  
           {      
               PartialView1ViewModel model;  
               public ParentPageViewModel()  
               {  
                   model = new PartialView1ViewModel { Posts = new List<Post>() };  
               }  
    
    
               void somewhereYouWantToAccess()  
               {  
                   var list = model.Posts;  
               }  
    
           }  
    

    Thank you.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.