我正在尝试获取其个人资料中包含博客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()以在我的视图中显示?
感谢。
答案 0 :(得分: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