title: MVC ViewModel errors
body: <p>Goal: To create a re-usable drop down menu that lists my website's administrators, managers and agents. These types of users are defined by the .NET Simplemembership webpages_Roles and webpages_UsersInRoles tables.</p>
  
  <p>So Far:
  I have a UserProfile table in my database which has 25 columns. I have a corresponding domain model of the same name which is accessed from my UsersContext() EF.</p>
  
  <p>The drop down menu only needs to list the User's FirstName, LastName and UserId so instead of working with the complete domain model, I created the following ViewModel:</p>
  
  <pre><code>namespace MyModels.Models.ViewModels
  {
      public class AdminsAndAgentsListVM
      {
          public string FirstName { get; set; }
          public string LastName { get; set; }
          public int UserId { get; set; }
      }
  }
  </code></pre>
  
  <p>I then added the following to my Account controller (notice I'm not working with partial view yet):</p>
  
  <pre><code>public ActionResult AdminsAndAgentsList()
  {
     UsersContext _db = new UsersContext(); //provides me access to UserProfiles data
     var admins = Roles.GetUsersInRole("Admin"); //gets users with this role
  
     var viewModel = _db.UserProfiles
         .Where(x =&gt; admins.Contains(x.UserName)); //Selects users who match UserName list
      return View(viewModel);
  }
  </code></pre>
  
  <p>I then scaffold a list view and base it on the strongly typed ViewModel:</p>
  
  <pre><code>    @model IEnumerable&lt;MyModels.Models.ViewModels.AdminsAndAgentsListVM&gt;
  
  @{
      ViewBag.Title = "AdminsAndAgentsList";
  }
  
  &lt;h2&gt;AdminsAndAgentsList&lt;/h2&gt;
  
  &lt;p&gt;
      @Html.ActionLink("Create New", "Create")
  &lt;/p&gt;
  &lt;table&gt;
      &lt;tr&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.FirstName)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.LastName)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.UserId)
          &lt;/th&gt;
          &lt;th&gt;&lt;/th&gt;
      &lt;/tr&gt;
  
  @foreach (var item in Model) {
      &lt;tr&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.FirstName)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.LastName)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.UserId)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
              @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
              @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
          &lt;/td&gt;
      &lt;/tr&gt;
  }
  
  &lt;/table&gt;
  </code></pre>
  
  <p>I do a successful build and when I run the web page I get the following error:</p>
  
  <blockquote>
    <p>The model item passed into the dictionary is of  type'System.Data.Entity.Infrastructure.DbQuery<code>1[My.Models.UserProfile]',
    but this dictionary requires a model item of type
    'System.Collections.Generic.IEnumerable</code>1[My.Models.ViewModels.AdminsAndAgentsListVM]'.</p>
  </blockquote>
  
  <p>If I recreate the view but strongly type it agains the UserProfile, it works fine. So how to re work this so I can strongly type against my ViewModel instead? Please provide examples if possible. I am new to C# and MVC and really benefit from the seeing the code first hand. Much appreciate the help!</p>
  
  <p>EDIT -----------------------------
  Here is the object for the UserProfile:</p>
  
  <pre><code>public class UsersContext : DbContext
  {
      public UsersContext()
          : base("DefaultConnection")
  
      {
      }
  
      public DbSet&lt;UserProfile&gt; UserProfiles { get; set; }
  }
  
  [Table("UserProfile")]
  public class UserProfile
  {
  
      [Key]
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
      public int UserId { get; set; }
      public string UserName { get; set; }
  
      [Required]
      [ReadOnly(true)]
      [DisplayName("SubscriberID")]
      public int? SubscriberId { get; set; } //Foreign key
  
      [StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long.")]
      [Display(Name = "First Name")]
      public string FirstName { get; set; }
  
      [StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long.")]
      [Display(Name = "Last Name")]
      public string LastName { get; set; }
  
      //public DateTime DOB { get; set; }
      [DataType(DataType.Date)]
      public DateTime? DOB { get; set; } //This allows null
  
      public bool? Gender { get; set; }
  
      [Required]
      [MaxLength(250)]
      [EmailAddress]
      public string Email { get; set; }
  
      [MaxLength(250)]
      [EmailAddress]
      [NotEqualTo("Email", ErrorMessage = "Alt Email and Email cannot be the same.")]
      public string AltEmail { get; set; }
  
      [MaxLength(250)]
      [EmailAddress]
      public string FormEmail { get; set; }
  
      public Address Address { get; set; }
  
      [MaxLength(20)]
      public string Telephone { get; set; }
  
      [MaxLength(20)]
      public string Mobile { get; set; }
  
      [Required]
      [DataType(DataType.Date)]
      public DateTime DateAdded { get; set; }
  
      [DataType(DataType.DateTime)]
      public DateTime? LastLoginDate { get; set; }
  
      public bool? OffersOptIn { get; set; } //any offers we may have with us or partners
  
      public bool? NewsOptIn { get; set; } //newsletter
  
      public bool? SubscriptionOptIn { get; set; } //account, technical, renewal notices, pp invoices, pp receipts
  
      public bool? OrderOptIn { get; set; } //orders - workflow notices
  
      [DataType(DataType.DateTime)]
      public DateTime? LastUpdatedAccountDate { get; set; } //Last time user updated contact info
  }
  </code></pre>