Files
Vidly2/Vidly/Controllers/CustomersController.cs

106 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Web.Mvc;
using Vidly.Models;
using Vidly.ViewModels;
namespace Vidly.Controllers
{
public class CustomersController : Controller
{
private ApplicationDbContext _context;
public CustomersController()
{
_context = new ApplicationDbContext();
}
protected override void Dispose(bool disposing)
{
_context.Dispose();
}
//// GET: Vidly
//public ActionResult Index()
//{
// return View();
//}
public ActionResult New()
{
var membershipTypes = _context.MembershipTypes.ToList();
var viewModel = new CustomerFormViewModel
{
Customer = new Customer(),
MembershipTypes = membershipTypes
};
return View("CustomerForm",viewModel);
}
[HttpPost]
public ActionResult Save(Customer customer)
{
if(customer.Id==0)
_context.Customers.Add(customer);
else
{
var customerInDb = _context.Customers.Single(c => c.Id == customer.Id);
// Mapper.Map(customer, customerInDb);
customerInDb.Name = customer.Name;
customerInDb.BirthDate = customer.BirthDate;
customerInDb.MembershipTypeId = customer.MembershipTypeId;
customerInDb.IsSubscribedToNewsLetter = customer.IsSubscribedToNewsLetter;
}
_context.SaveChanges();
return RedirectToAction("Index","Customers");
}
[Route("Customers")]
[Route("Customers/Index")]
public ActionResult Index()
{
var viewModel = new CustomersViewModel()
{
Customers = _context.Customers.Include(c=>c.MembershipType).ToList()
};
return View(viewModel);
}
public ActionResult Customer(int nr)
{
var customer = _context.Customers.Include(c => c.MembershipType).SingleOrDefault(c => c.Id == nr);
if (customer== null)
return HttpNotFound();
return View(customer);
}
public ActionResult Edit(int id)
{
var customer = _context.Customers.SingleOrDefault(c=>c.Id==id);
if (customer == null)
return HttpNotFound();
var viewModel = new CustomerFormViewModel
{
Customer = customer,
MembershipTypes = _context.MembershipTypes.ToList()
};
return View("CustomerForm", viewModel);
}
//public ActionResult Movies()
//{
// return View();
//}
}
}