Search code examples
vb.netasp.net-mvc-5.1navigation-propertiesentity-framework-6.1

How to show value of a navigation property with Entity Framework and MVC?


I have the following query and am trying to show the Category name(s) in the index view. I can verify in LinqPad that the category is getting filled for each post.

I have tried @item.PostCategory which shows System.Collections.Generic.HashSet1[be_Categories] while @item.PostCategory.FirstOrDefault.CategoryName throws Object reference not set to an instance of an object. And I can verify that each post has the correct number of categories associated with it via @item.PostCategory.Count.

How exactly do i show the value of be_Categories for each post in the index view?

 Public Function SelectAll() As IQueryable(Of PostSummaryDTO) 
   Implements IPostRepository.SelectAll
   db = New BetterBlogContext
   Dim posts As IQueryable(Of PostSummaryDTO) = db.be_Posts.OrderByDescending
   (Function(x)x.DateCreated).Include(Function(c)
   c.be_Categories).Select(Function(s) New PostSummaryDTO With {.PostId = s.PostRowID,
   .PostDateCreated = s.DateCreated, .PostText = s.PostContent, .PostGuid = s.PostID,
   .PostSummary = s.Description, .PostCategory = s.be_Categories, .PostTitle = s.Title,
   .PostIsPublished = s.IsPublished, .PostTag = s.be_PostTag})

    Return posts

enter image description here

Entity:

Partial Public Class be_Posts    
        <Key>
        Public Property PostRowID As Integer    
        Public Property BlogID As Guid    
        Public Property PostID As Guid    
        <StringLength(255)>
        Public Property Title As String    
        Public Property Description As String    
        <AllowHtml> Public Property PostContent As String    
        Public Property DateCreated As Date?    
        Public Property DateModified As Date?    
        <StringLength(50)>
        Public Property Author As String    
        Public Property IsPublished As Boolean?    
        Public Property IsCommentEnabled As Boolean?    
        Public Property Raters As Integer?    
        Public Property Rating As Single?    
        <StringLength(255)>
        Public Property Slug As String    
        Public Property IsDeleted As Boolean    
        Public Overridable Property be_PostTag As ICollection(Of be_PostTag)    
        <ForeignKey("be_Categories")> Public Overridable Property be_Categories As ICollection(Of be_Categories)    
        Public Property be_PostCategory As ICollection(Of be_PostCategory)    
    End Class

Solution

  • Ok this seems to work - needed a second for each loop in the index view:

     @For Each x In item.PostCategory
        @x.CategoryName
     Next
    

    The whole code:

        <div class="row">
            @For Each item In Model
              @<div class="col-xs-6 col-sm-6 col-lg-6 col-md-6">
                 <div class="panel panel-default panel-body content">
                   <b>@item.PostDateCreated.ToShortDateString <span class="pull-right">@item.PostDateCreated.AddHours(3).ToShortTimeString</span></b>
                     @For Each x In item.PostCategory
                        @x.CategoryName
                      Next
                        <div class="galleryImgWrapper">
                           <a href="@Url.Action("Details", "Posts", New With {.id = item.Id, .title = BetterBlog.Core.Helpers.SeoHelper.ToSeoUrl(item.PostTitle)}, Nothing)">
                            @Html.Raw(item.PostSummary.GetImage)
                            </a>
                            </div>
                            <h4>@Html.ActionLink(item.PostTitle, "Details", "Posts", 
    New With {.id = item.Id, .title = ToSeoUrl(item.PostTitle)}, Nothing)</h4>
                      @Html.Raw(item.PostSummary.RemoveImgWithRegex)
                      </div>
                    </div>
                    @<div class="clear"></div>
                Next
            </div>