-1

我正在嘗試生成系統中所有用戶的列表。我正在使用默認的內置身份驗證。在我的asp mvc視圖中使用什麼模型

,這裏是我的身份識別模型類:

public class ApplicationUser : IdentityUser 
{ 
    [Required] 
    [MaxLength(50)] 
    public string email { get; set; } 

    [Required] 
    [StringLength(11, MinimumLength = 11)] 
    public string cellNo { get; set; } 

    [Required] 
    [MaxLength(50), MinLengthAttribute(3)] 
    public string firstName { get; set; } 

    [Required] 
    [MaxLength(50), MinLengthAttribute(3)] 
    public string lastName { get; set; } 

    public DateTime birthdate { get; set; } 

} 

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
    public ApplicationDbContext() 
     : base("AuthenticationConnection") 
    { 
    } 
} 

現在我有創建一個控制器進行調用用戶:

public class UsersController : Controller 
{ 
    private User_Manager_Interface.Models.ApplicationDbContext userDb = new User_Manager_Interface.Models.ApplicationDbContext(); 

    // GET: /Users/ 
    public ActionResult Index() 
    { 
     return View(userDb.Users.ToList()); 
    } 
    } 

1: 我用我在我的控制器正確DATABSE背景?

2: 我認爲我應該使用什麼模型?目前,我有以下幾點: @model IEnumerable<User_Manager_Interface.Models.ApplicationDbContext>但它給我一個錯誤,當我運行它:錯誤如下:

傳遞到字典的模型產品 型「的System.Collections.Generic.List 1[User_Manager_Interface.Models.ApplicationUser]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1 [User_Manager_Interface.Models.ApplicationDbContext]'。

+0

請檢查綁定到您的視圖的模型以及您傳遞的是哪個模型! –

回答

0

您從您的控制器傳遞到ApplicationUser列表視圖。錯誤消息中提到了相同的內容。因此,要處理視圖中的用戶列表,您應該使用適當的類型

@model IEnumerable<User_Manager_Interface.Models.ApplicationUser> 
+0

哦,我明白了。我不能將視圖綁定到上下文。它需要成爲一個模型.. – Zapnologica

+0

爲什麼我的模型現在發出一個恐怖? '@ H​​tml.DisplayNameFor(model => model.firstName)'當我完全停止時,我沒有任何選項>並且我在模型上添加了顯示標記? – Zapnologica

0

在你看來

@model List<User_Manager_Interface.Models.ApplicationUser> 
+0

'傳入字典的模型項目類型爲'System.Collections.Generic.List'1 [User_Manager_Interface.Models.ApplicationUser]',但是這個字典需要一個'System.Collections.Generic.List'1類型的模型項[User_Manager_Interface.Models.ApplicationDbContext]'。' – Zapnologica

+0

其錯誤..編輯答案 – Nilesh

1

您需要解析式可以轉化爲模型試試這個。

由於您返回List<User_Manager_Interface.Models.ApplicationUser>您需要設置支持該模型。

@model IList<User_Manager_Interface.Models.ApplicationUser> 
@model IEnumerable<User_Manager_Interface.Models.ApplicationUser> 
@model List<User_Manager_Interface.Models.ApplicationUser> 

有很多的可能性,但上述類型之一應該工作。

+0

你有沒有可能詳細說明你所建議的3個選項之間的差異? – Zapnologica

相關問題