0

我正在尝试获取在其个人资料中具有博客 ID 的用户的用户名。

此代码返回所有博客并将其放入列表中:

        Dim blogs = db.Blogs.Include(Function(b) b.Company)
        Return View(blogs.ToList())

我想在列表中包含博客所属的用户名。该数据保存在配置文件实体中(在名为“BlogId”的字段中)。

这是配置文件实体:

Imports System.Data.Entity
Imports System.ComponentModel.DataAnnotations

Public Class UserProfile

    Public Property UserProfileId() As Integer
    Public Property UserId() As Guid
    Public Property CompanyId() As Integer
    Public Property BlogId() As Integer
    Public Property IsCompanyOwner As Boolean
    Public Property IsBlogOwner As Boolean

End Class

Public Class UserProfileDbContext

    Inherits DbContext
    Public Property UserProfiles As DbSet(Of UserProfile)
End Class

如何将用户名放入 ToList() 以在我的视图中显示?

谢谢。

4

1 回答 1

0

如果您指的是会员资格的用户,在这种情况下,请使用您的 UserId 指南调用:

var user = Membership.GetUser(userId)

然后您可以将名称引用为

user.UserName

因此,理想情况下,您需要创建一个新的 ViewModel 类,该类只包含您要发送到视图的数据,而没有其他内容。在这种情况下,加载您的 UserProfile 条目后,您需要枚举每个条目并调用 GetUser() 并使用该结果填充每个 ViewModel 条目。



Public Class UserProfileViewModel

    Public Property UserProfileId() As Integer
    Public Property UserId() As Guid
    Public Property CompanyId() As Integer
    Public Property BlogId() As Integer
    Public Property IsCompanyOwner As Boolean
    Public Property IsBlogOwner As Boolean
    **Public Property UserName as String**

End Class
于 2012-08-02T21:24:48.900 回答