2012-08-08 41 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