2012-08-08 39 views
0

如何连接到会员数据库以检索用户帐户列表?例如我有这个连接到我的配置文件:MVC成员数据库上下文?

Private db As UserProfileDbContext = New UserProfileDbContext 

    ' 
    ' GET: /UserProfile/ 

    Function Index() As ViewResult 
     Return View(db.UserProfiles.ToList()) 
    End Function 

似乎没有在帐户模型中指定任何用户帐户数据库上下文。我应该创建一个,还是有更好的方法来检索所有用户帐户到上面的列表中?

编辑:

我有这样的代码在控制器:

' 
' GET: /Account/ViewRegistries 

Function ViewRegistries() As ViewResult 
    Return View(Membership.GetAllUsers().Cast(Of MembershipUser).ToList) 
End Function 

我的观点:

@ModelType IEnumerable(Of MyBlog.RegisterModel) 

@Code 
    ViewData("Title") = "Index" 
End Code 

<h2>Index</h2> 

<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 
<table> 
    <tr> 
     <th> 
      UserId 
     </th> 
     <th> 
      CompanyId 
     </th> 
     <th></th> 
    </tr> 

@For Each item In Model 
    Dim currentItem = item 
    @<tr> 
     <td> 
      @Html.DisplayFor(Function(modelItem) currentItem.UserName) 
     </td> 
     <td> 
      @Html.DisplayFor(Function(modelItem) currentItem.Company) 
     </td> 
    </tr> 
Next 

</table> 

但它会产生一个错误:

The model item passed into the dictionary is of type 'System.Collections.Generic.List 1[System.Web.Security.MembershipUser]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1[MyBlog.RegisterModel]'.

哪有我修复它?

+1

只需创建一个,或去登录,注册和注册在那里。由于没有用户在场,所以一开始你什么都没有。 您将不得不创建它。 – Rajesh 2012-08-08 15:43:03

+0

你也可以尝试在visual studio中使用aspnet配置来创建它。 另外,您可以在Visual Studio命令提示符 – Rajesh 2012-08-08 15:44:14

+1

@Rajesh使用aspnet_regsql.exe的:当我发布到服务器我还可以使用ASPNET配置?它看起来没有密码保护。 – user1477388 2012-08-08 15:45:11

回答

2

您可以使用成员资格提供:

Membership.GetAllUsers() 

一旦你注册用户,这将在数据库中返回所有用户的列表。

+0

是的。达林是对的。但要确保连接字符串是正确的。然后在数据库中注册一些用户。 – Rajesh 2012-08-08 15:45:56

+0

这会进入我的模型,并以与db.UserProfiles.ToList()相同的方式使用吗?或者我会不得不做更多的工作? – user1477388 2012-08-08 15:54:00

+1

这将查询成员资格提供者并返回'MembershipUser'列表。如果你想获得用户名列表,只需使用LINQ:'Dim usernames = Membership.GetAllUsers()。Cast(Of MembershipUser).Select(Function(u)u.UserName).ToList()'。 – 2012-08-08 15:55:43