First Last

First Last

  • 1.6k
  • 613
  • 45.9k

How to take a subset of a views Model and send the subset model

Aug 18 2020 3:56 PM
How do I take a subset of a views Model and send the subset model to a partial view?
I created a model, but the partial is looking for the parent model.
Am I creating this secondary subset model correctly? How do I get the partial to see this secondary subset model?
Parent View code (not all):
  1. @model GbngWebClient.Models.BlogPublishedByBlogIdVM    
  2.     
  3. @* Create a subset model to send to the partial view. *@    
  4. @{    
  5.      int parentBlogId = @Model.BlogPublishedByBlogId.BlogId;    
  6.      int parentLikeCount = @Model.BlogPublishedByBlogId.LikeCount;    
  7.      int parentDisLikeCount = @Model.BlogPublishedByBlogId.DisLikeCount;    
  8.      bool parentDisabledBoolean =     
  9.      @Model.BlogPublishedByBlogId.DisabledBoolean;    
  10. }    
  11.     
  12. <div class="row">    
  13.             <div class="col-md-1">    
  14.                 @* A partial view. Sending the subset model to the partial view. *@    
  15.                 @Html.Partial("_BlogLikeAndDislike", @Model)    
  16.             </div>    
  17. </div>    
  18. <br />   
Partial view code (not all):
  1. <div>    
  2. @* Get the parent view's values that were passed via a model. *@    
  3.    <i class="BlogLike fa fa-my-size fa-thumbs-up"></i> | <i> @Model.LikeCount</i>    
  4.    <i class="BlogDisLike fa fa-my-size fa-thumbs-down"></i> | <i> @Model.DisLikeCount</i>    
  5. </div>  

Answers (3)