-1

我正在尝试生成系统中所有用户的列表。我正在使用默认的内置身份验证。

这是我的身份模型类:

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")
    {
    }
}

现在我创建了一个名为 users 的控制器:

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:我是否在控制器中使用了正确的数据库上下文?

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.IEnumerable1[User_Manager_Interface.Models.ApplicationDbContext]”。

4

3 回答 3

1

您需要解析一种可以转换为模型的类型。

由于您返回 aList<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>

有很多可能性,但上述类型中的一种应该可以工作。

于 2014-01-29T09:47:30.533 回答
0

在你看来试试这个

@model List<User_Manager_Interface.Models.ApplicationUser>
于 2014-01-29T09:42:44.537 回答
0

您从控制器传递到应用程序用户列表的视图。错误消息中提到的相同。因此,要处理视图中的用户列表,您应该使用适当的类型

 @model IEnumerable<User_Manager_Interface.Models.ApplicationUser>
于 2014-01-29T09:48:26.173 回答