title: How to map values from a database to a view model in MVC 4
body: <p>I am having trouble with what should be a very simple task. I'm trying to map values retrieved from a database to a view model. I then want to pass that as the model to my view.</p>
  
  <p>The view model is called EditAdminModelVM and it contains a smaller subset of properties from a larger class called UserProfile. I chose to use a view model because I do not want the user to be able to update the whole domain model, only a few from it.</p>
  
  <p>Here is the ViewModel:</p>
  
  <pre><code>public class EditAdminModelVM
  {
      [Required]
      [StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long.")]
      [Display(Name = "First name")]
      public string FirstName { get; set; }
  
      [Required]
      [StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long.")]
      [Display(Name = "Last name")]
      public string LastName { get; set; }
  
      [Required]
      [MaxLength(250)]
      [EmailAddress]
      public string Email { get; set; }
  
      [Required]
      //[Remote("doesUserNameExist", "Account", HttpMethod = "POST", ErrorMessage = "Username already exists. Please enter a different User Name.")] //note this checks from client-side only - server side check is built in to membership
      [Display(Name = "Username")]
      public string UserName { get; set; }
  
      [HiddenInput]
      public int UserId { get; set; }
  }
  </code></pre>
  
  <p>Here is the controller 9though it does not work):</p>
  
  <pre><code>[Authorize(Roles = "Admin")]
      public ActionResult EditAdmin(int id = 0)
      {
          PpDB db = new PpDB();
  
          var viewModel = new EditAdminModelVM();
  
          viewModel = db.UserProfiles
               .Where(x =&gt; x.UserId == id)
               .Select(x =&gt; new EditAdminModelVM
               {
                   FirstName = x.FirstName,
                   LastName = x.LastName,
                   Email = x.Email,
                   UserName = x.UserName
               });
  
          return View(viewModel);
      }
  </code></pre>
  
  <p>In this case the .Select is underlined with a squiggle which says something about IQueryable. But if I try to set var viewModel = new IQueryable; I get another error.</p>
  
  <p>I suppose this could be a syntax problem. This DB query should only return one row so I did not think IQueryable is necessary, but I am no expert.</p>
  
  <p>If you could please provide me with a working a sample of how to construct this code block, then I could learn by example - better for my tired brain.</p>
  
  <p>Much appreciated!</p>
  
  <p><strong>UPDATE FOR KEN</strong>
  Here is the updated controller:</p>
  
  <pre><code> [Authorize(Roles = "Admin")]
      public ActionResult EditAdmin(int id = 0)
      {
          PpDB db = new PpDB();
  
          List&lt;EditAdminModelVM&gt; viewModel = new List&lt;EditAdminModelVM&gt;();
  
          viewModel = db.UserProfiles
               .Where(x =&gt; x.UserId == id)
               .Select(x =&gt; new EditAdminModelVM
               {
                   FirstName = x.FirstName,
                   LastName = x.LastName,
                   Email = x.Email,
                   UserName = x.UserName,
                   UserId = x.UserId
               }).ToList();
  
          return View(viewModel);
      }
  </code></pre>
  
  <p>And here is the view:</p>
  
  <pre><code>@model List&lt;PpModels.Models.EditAdminModelVM&gt;
  
  
  @{
      ViewBag.Title = "Edit Admin";
      Layout = "~/Views/Shared/_Layout.cshtml";
  }
  
  @using (Html.BeginForm()) {
      @Html.AntiForgeryToken()
      @Html.ValidationSummary(true)
  
  &lt;fieldset&gt;
      &lt;legend&gt;UserProfile&lt;/legend&gt;
  
      @Html.HiddenFor(model =&gt; model.UserId)
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.UserName)
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.EditorFor(model =&gt; model.UserName)
          @Html.ValidationMessageFor(model =&gt; model.UserName)
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.FirstName)
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.EditorFor(model =&gt; model.FirstName)
          @Html.ValidationMessageFor(model =&gt; model.FirstName)
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.LastName)
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.EditorFor(model =&gt; model.LastName)
          @Html.ValidationMessageFor(model =&gt; model.LastName)
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.Email)
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.EditorFor(model =&gt; model.Email)
          @Html.ValidationMessageFor(model =&gt; model.Email)
  
      &lt;p&gt;
          &lt;input type="submit" value="Save" /&gt;
      &lt;/p&gt;
  &lt;/fieldset&gt;
  }
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  
  @section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
  }
  </code></pre>