2012-08-02 129 views
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(),以显示它在我的看法?

谢谢。

+0

我在UserProfile对象的任何地方都看不到用户名。您是否在公司对象中投射到结果中? – JustinMichaels 2012-08-02 19:59:39

+0

UserId是Users实体的外键。博客对象有一个名为“公司”的字段,但这不相关。 – user1477388 2012-08-02 20:03:06

+0

你能不能在代码中发布你的用户对象? – JustinMichaels 2012-08-02 20:08:43

回答

0

如果你指的是会员的用户,在这种情况下,使用您的用户ID指南拨打:

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 
+0

所以,我不能将两个模型链接在一起,我必须创建一个单独的模型? – user1477388 2012-08-03 12:20:04

+0

我的意思是,我无法将控制器中的两个模型链接在一起并将它们发送到视图?相反,我需要创建一个包含视图中所需信息的独立模型? – user1477388 2012-08-03 12:51:01

相关问题