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:
My app.Domain project
I have a user table in the database and reference it with Entities/Users.cs
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; }
}
}
Next, I have an interface and it is located Abstract/IUsersRepository.cs
using System;
using System.Collections.Generic;
using app.Domain.Entities;
namespace app.Domain.Abstract
{
public interface IUsersRepository
{
IEnumerable<Users> Users { get; }
}
}
Moving along, now I fill my entities Concrete/EFUsersRepository.cs
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<Users> Users
{
get { return context.Users; }
}
}
}
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.
Here is my app.WebUI solution:
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)?
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<Users> Users { get; set; }
}
}
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).
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 => p.Email == "[email protected]")
};
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();
}
}
}
And here is my View (Home/Index.cshtml):
@model app.WebUI.Models.UsersViewModel
@{
ViewBag.Title = "Home Page";
Layout = "~/Views/Shared/_LayoutNoMenu.cshtml";
}
@foreach (var p in Model.Users)
{
<div class="container">
@using (Html.BeginForm("About", "Home", FormMethod.Get, new { @class = "begin-form" }))
{
<h1>Welcome</h1>
<div class="required-field-block">
<textarea rows="1" class="form-control" placeholder="Email" id="filter"></textarea>
</div>
<button class="btn btn-primary" type="submit">Login</button>
}
</div>
}
Any advice on how to correctly use a ViewModel?