title: MVC ViewModel example
body: <p>I've been doing tutorials and trying to learn best practice when it comes to MVC development.  The design I'm using below comes from Pro ASP.Net MVC5 by Apress/Adam Freeman.  So far, everything is coming along good...but I still have not completely come to grip on working with Controllers.  Yes, I understand the concept of Controllers, but still struggle when it comes to post and get methods.  Here is the flow of my sample MVC application:</p>
  
  <p>My app.Domain project</p>
  
  <p>I have a user table in the database and reference it with Entities/Users.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  
  namespace app.Domain.Entities
  {
  public class Users
  {
      [Key]
      public int UserID { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string Email { get; set; }
      public string City { get; set; }
      public string State { get; set; }
      public DateTime CreateDate { get; set; }
      public DateTime LastLogin { get; set; }
  
  }
  }
  </code></pre>
  
  <p>Next, I have an interface and it is located Abstract/IUsersRepository.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using app.Domain.Entities;
  
  namespace app.Domain.Abstract
  {
  public interface IUsersRepository
  {
      IEnumerable&lt;Users&gt; Users { get; }
  }
  }
  </code></pre>
  
  <p>Moving along, now I fill my entities Concrete/EFUsersRepository.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using app.Domain.Entities;
  using app.Domain.Abstract;
  
  namespace app.Domain.Concrete
  {
  public class EFUsersRepository : IUsersRepository
  {
      private EFDbContext context = new EFDbContext();
  
      public IEnumerable&lt;Users&gt; Users
      {
          get { return context.Users; }
      }
  }
  }
  </code></pre>
  
  <p>Also, the textbook is using Ninject which I understand and everything is bound correctly.  I won't post that code unless someone asks me to.</p>
  
  <p>Here is my app.WebUI solution:</p>
  
  <p>The textbook walks me through creating a ViewModel.  This is where things get a little fuzzy for me.  Is the ViewModel an additional channel to get the entities?  Instead of referencing the Models themselves, should I always create ViewModels to SELECT, UPDATE, INSERT, DELETE data (Models/UsersViewModel.cs)?</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using app.Domain.Entities;
  
  namespace app.WebUI.Models
  {
  public class UsersViewModel
  {
      //public string FirstName { get; set; }
      //public string LastName { get; set; }
      //public string Email { get; set; }
      //public string City { get; set; }
      //public string State { get; set; }
      public IEnumerable&lt;Users&gt; Users { get; set; }
  }
  }
  </code></pre>
  
  <p>The scenario is for the user to type in an email, then the Controller checks the database for the email.  If it exist, then redirect to the About View (Controllers/HomeController.cs). </p>
  
  <pre><code>using System.Linq;
  using System.Web.Mvc;
  using app.Domain.Abstract;
  using app.WebUI.Models;
  
  
  namespace app.Controllers
  {
  public class HomeController : Controller
  {
      private IUsersRepository repository;
  
      public HomeController(IUsersRepository usersRepository)
      {
          this.repository = usersRepository;
      }
  
      [HttpGet]
      public ActionResult Index()
      {
          return View();
      }
  
      [HttpPost]
      public ActionResult Index()
      {
          UsersViewModel userViewModel = new UsersViewModel()
          {
              Users = repository.Users
              .Where(p =&gt; p.Email == "LearningMVC5@gmail.com")
          };
          return View("About", userViewModel);
  
      }
  
      public ActionResult About()
      {
          ViewBag.Message = "Your application description page.";
          return View();
      }
  
      public ActionResult Contact()
      {
          ViewBag.Message = "Your contact page.";
          return View();
      }
  }
  }
  </code></pre>
  
  <p>And here is my View (Home/Index.cshtml):</p>
  
  <pre><code>@model app.WebUI.Models.UsersViewModel
  
  @{
      ViewBag.Title = "Home Page";
      Layout = "~/Views/Shared/_LayoutNoMenu.cshtml";
  }
  
  
  @foreach (var p in Model.Users)
  { 
  &lt;div class="container"&gt;
  @using (Html.BeginForm("About", "Home", FormMethod.Get, new { @class = "begin-form" }))
  {
      &lt;h1&gt;Welcome&lt;/h1&gt;
      &lt;div class="required-field-block"&gt;
      &lt;textarea rows="1" class="form-control" placeholder="Email" id="filter"&gt;&lt;/textarea&gt;
      &lt;/div&gt;
      &lt;button class="btn btn-primary" type="submit"&gt;Login&lt;/button&gt;
  }
  &lt;/div&gt;
  }
  </code></pre>
  
  <p>Any advice on how to correctly use a ViewModel?</p>