Question

I am trying to generate a list of all the users in the system. I am using the default built in authentication.

here is my Identity models class:

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

Now i have creates a controller called 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: Am i using the correct databse context in my controller?

2: What model must i use in my view? Currently I have the following: @model IEnumerable<User_Manager_Interface.Models.ApplicationDbContext> But it give me an error when I run it: THe error is as follows:

The model item passed into the dictionary is of type 'System.Collections.Generic.List1[User_Manager_Interface.Models.ApplicationUser]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[User_Manager_Interface.Models.ApplicationDbContext]'.

Was it helpful?

Solution 2

You pass from your controller to the view the list of ApplicationUser. The same mentioned in error message. So, to process the list of users in your view you should use appropriate type

 @model IEnumerable<User_Manager_Interface.Models.ApplicationUser>

OTHER TIPS

You need to parse a type that can be converted into the model.

Since you return a List<User_Manager_Interface.Models.ApplicationUser> you need to set a model that supports that.

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

There is lots of possibilities, but one of the above types should work.

Try this in your view

@model List<User_Manager_Interface.Models.ApplicationUser>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top