Reading from database

This commit is contained in:
2019-01-19 17:08:56 +01:00
parent 25dc803cfd
commit 19fc2475fa
14 changed files with 607 additions and 13 deletions

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Web.Mvc;
using Vidly.Models;
using Vidly.ViewModels;
@ -11,9 +12,12 @@ namespace Vidly.Controllers
public class CustomersController : Controller
{
List<Customer> customers = null;
private ApplicationDbContext _context;
public CustomersController()
{
_context = new ApplicationDbContext();
customers = new List<Customer>
{
new Customer {Name="Nils Persson", Id=1},
@ -22,6 +26,11 @@ namespace Vidly.Controllers
};
}
protected override void Dispose(bool disposing)
{
_context.Dispose();
}
// GET: Vidly
public ActionResult Index()
{
@ -33,7 +42,7 @@ namespace Vidly.Controllers
var viewModel = new CustomersViewModel()
{
Customers = customers
Customers = _context.Customers.Include(c=>c.MembershipType).ToList()
};
return View(viewModel);
@ -42,14 +51,10 @@ namespace Vidly.Controllers
[Route("Customers/Details/{nr}")]
public ActionResult Customer(int nr)
{
foreach(var cust in customers)
{
if (cust.Id == nr)
{
return View(cust);
}
}
return HttpNotFound();
var customer = _context.Customers.SingleOrDefault(c => c.Id == nr);
if (customer== null)
return HttpNotFound();
return View(customer);
}
public ActionResult Movies()