diff --git a/Vidly/App_Start/RouteConfig.cs b/Vidly/App_Start/RouteConfig.cs index 624cafa..58972ef 100644 --- a/Vidly/App_Start/RouteConfig.cs +++ b/Vidly/App_Start/RouteConfig.cs @@ -24,7 +24,7 @@ namespace Vidly routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", - defaults: new { controller = "Customers", action = "Index", id = UrlParameter.Optional } + defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } } diff --git a/Vidly/Controllers/CustomersController.cs b/Vidly/Controllers/CustomersController.cs index e878d25..85cb5ac 100644 --- a/Vidly/Controllers/CustomersController.cs +++ b/Vidly/Controllers/CustomersController.cs @@ -23,13 +23,46 @@ namespace Vidly.Controllers _context.Dispose(); } - // GET: Vidly - public ActionResult Index() + //// GET: Vidly + //public ActionResult Index() + //{ + // return View(); + //} + + public ActionResult New() { - return View(); + var membershipTypes = _context.MembershipTypes.ToList(); + var viewModel = new CustomerFormViewModel + { + Customer = new Customer(), + MembershipTypes = membershipTypes + }; + return View("CustomerForm",viewModel); } - public ActionResult Customers() + [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() @@ -40,7 +73,6 @@ namespace Vidly.Controllers return View(viewModel); } - [Route("Customers/Details/{nr}")] public ActionResult Customer(int nr) { var customer = _context.Customers.Include(c => c.MembershipType).SingleOrDefault(c => c.Id == nr); @@ -49,9 +81,26 @@ namespace Vidly.Controllers return View(customer); } - public ActionResult Movies() + public ActionResult Edit(int id) { - return View(); + 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(); + //} } } \ No newline at end of file diff --git a/Vidly/Controllers/HomeController.cs b/Vidly/Controllers/HomeController.cs index 90aed9d..47477b5 100644 --- a/Vidly/Controllers/HomeController.cs +++ b/Vidly/Controllers/HomeController.cs @@ -8,6 +8,10 @@ namespace Vidly.Controllers { public class HomeController : Controller { + + [Route("")] + [Route("Home")] + [Route("Home/Index")] public ActionResult Index() { return View(); diff --git a/Vidly/Controllers/MoviesController.cs b/Vidly/Controllers/MoviesController.cs index 0de9316..3188e9d 100644 --- a/Vidly/Controllers/MoviesController.cs +++ b/Vidly/Controllers/MoviesController.cs @@ -18,7 +18,14 @@ namespace Vidly.Controllers _context = new ApplicationDbContext(); } - public ActionResult Movies() + protected override void Dispose(bool disposing) + { + _context.Dispose(); + } + + [Route("Movies")] + [Route("Movies/Index")] + public ActionResult Index() { var viewModel = new MoviesViewModel() { diff --git a/Vidly/Models/Customer.cs b/Vidly/Models/Customer.cs index 2ac9284..56dd7de 100644 --- a/Vidly/Models/Customer.cs +++ b/Vidly/Models/Customer.cs @@ -9,12 +9,19 @@ namespace Vidly.Models public class Customer { public int Id { get; set; } + [Required] [StringLength(255)] public string Name { get; set; } + + [Display(Name = "Date of Birth")] public DateTime? BirthDate { get; set; } + public bool IsSubscribedToNewsLetter { get; set; } + public MembershipType MembershipType { get; set; } + + [Display(Name = "Membership Type")] public byte MembershipTypeId { get; set; } } } \ No newline at end of file diff --git a/Vidly/Models/IdentityModels.cs b/Vidly/Models/IdentityModels.cs index 4486710..6c83390 100644 --- a/Vidly/Models/IdentityModels.cs +++ b/Vidly/Models/IdentityModels.cs @@ -22,6 +22,7 @@ namespace Vidly.Models { public DbSet Customers { get; set; } public DbSet Movies { get; set; } + public DbSet MembershipTypes { get; set; } public ApplicationDbContext() // : base("DefaultConnection", throwIfV1Schema: false) diff --git a/Vidly/Vidly.csproj b/Vidly/Vidly.csproj index d427402..9422ed9 100644 --- a/Vidly/Vidly.csproj +++ b/Vidly/Vidly.csproj @@ -241,6 +241,7 @@ + @@ -278,7 +279,7 @@ - + @@ -301,11 +302,13 @@ + - - + + + diff --git a/Vidly/ViewModels/CustomerFormViewModel.cs b/Vidly/ViewModels/CustomerFormViewModel.cs new file mode 100644 index 0000000..cc827ac --- /dev/null +++ b/Vidly/ViewModels/CustomerFormViewModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using Vidly.Models; + +namespace Vidly.ViewModels +{ + public class CustomerFormViewModel + { + public IEnumerable MembershipTypes { get; set; } + public Customer Customer { get; set; } + } +} \ No newline at end of file diff --git a/Vidly/Views/Customers/CustomerForm.cshtml b/Vidly/Views/Customers/CustomerForm.cshtml new file mode 100644 index 0000000..cbf3994 --- /dev/null +++ b/Vidly/Views/Customers/CustomerForm.cshtml @@ -0,0 +1,40 @@ +@model Vidly.ViewModels.CustomerFormViewModel +@{ + /**/ + + ViewBag.Title = "New"; + Layout = "~/Views/Shared/_Layout.cshtml"; +} + +@if (Model.Customer.Id==null|| Model.Customer.Id == 0) +{ +

New Customer

+} +else +{ +

Edit Customer

+} + +@using (@Html.BeginForm("Save", "Customers")) +{ +
+ @Html.LabelFor(m => m.Customer.Name) + @Html.TextBoxFor(m => m.Customer.Name, new { @class = "form-control" }) +
+
+ @Html.LabelFor(m => m.Customer.BirthDate) + @Html.TextBoxFor(m => m.Customer.BirthDate, "{0:d MMM yyyy}", new { @class = "form-control" }) +
+
+ @Html.LabelFor(m => m.Customer.MembershipTypeId) + @Html.DropDownListFor(m => m.Customer.MembershipTypeId, new SelectList(Model.MembershipTypes, "Id", "Name"), "Select membership Type", new { @class = "form-control" }) +
+
+ +
+ @Html.HiddenFor(m => m.Customer.Id) + + +} diff --git a/Vidly/Views/Customers/Customers.cshtml b/Vidly/Views/Customers/Customers.cshtml deleted file mode 100644 index 0ebb0fb..0000000 --- a/Vidly/Views/Customers/Customers.cshtml +++ /dev/null @@ -1,33 +0,0 @@ -@model Vidly.ViewModels.CustomersViewModel -@{ - ViewBag.Title = "Customers"; - Layout = "~/Views/Shared/_Layout.cshtml"; -} - -

Customers

- -@if (Model.Customers.Count == 0) -{ - No one has rented this movie before. -} -else -{ - - - - - - - - - @foreach (var customer in Model.Customers) - { - - - - - } - -
CustomerMembership Type
@Html.ActionLink(@customer.Name, "Details", "Customers", new { id = @customer.Id }, null)@customer.MembershipType.Name
- -} \ No newline at end of file diff --git a/Vidly/Views/Customers/Index.cshtml b/Vidly/Views/Customers/Index.cshtml index 43f7c7f..58f00ea 100644 --- a/Vidly/Views/Customers/Index.cshtml +++ b/Vidly/Views/Customers/Index.cshtml @@ -1,11 +1,37 @@ - +@model Vidly.ViewModels.CustomersViewModel @{ - ViewBag.Title = "Vidly"; + ViewBag.Title = "Customers"; + Layout = "~/Views/Shared/_Layout.cshtml"; } -
-

V i d l y

-

Vidly is a great video rental system built in MVC5 with entity framework, Razorviews and Javascript.

- @*

Learn more »

*@ -
+

Customers

+@**@ +@Html.ActionLink("New Customer", "New", "Customers", null, new { @class = "btn btn-primary" }) + + +@if (Model.Customers.Count == 0) +{ + No one has rented this movie before. +} +else +{ + + + + + + + + + @foreach (var customer in Model.Customers) + { + + + + + } + +
CustomerMembership Type
@Html.ActionLink(@customer.Name, "Edit", "Customers", new { id = @customer.Id }, null)@customer.MembershipType.Name
+ +} \ No newline at end of file diff --git a/Vidly/Views/Customers/Old_Index.cshtml b/Vidly/Views/Customers/Old_Index.cshtml new file mode 100644 index 0000000..43f7c7f --- /dev/null +++ b/Vidly/Views/Customers/Old_Index.cshtml @@ -0,0 +1,11 @@ + +@{ + ViewBag.Title = "Vidly"; +} + +
+

V i d l y

+

Vidly is a great video rental system built in MVC5 with entity framework, Razorviews and Javascript.

+ @*

Learn more »

*@ +
+ diff --git a/Vidly/Views/Home/Index.cshtml b/Vidly/Views/Home/Index.cshtml index 32e1dd9..43f7c7f 100644 --- a/Vidly/Views/Home/Index.cshtml +++ b/Vidly/Views/Home/Index.cshtml @@ -1,31 +1,11 @@ -@{ - ViewBag.Title = "Home Page"; + +@{ + ViewBag.Title = "Vidly"; }
-

ASP.NET

-

ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.

-

Learn more »

+

V i d l y

+

Vidly is a great video rental system built in MVC5 with entity framework, Razorviews and Javascript.

+ @*

Learn more »

*@
-
-
-

Getting started

-

- ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that - enables a clean separation of concerns and gives you full control over markup - for enjoyable, agile development. -

-

Learn more »

-
-
-

Get more libraries

-

NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.

-

Learn more »

-
-
-

Web Hosting

-

You can easily find a web hosting company that offers the right mix of features and price for your applications.

-

Learn more »

-
-
\ No newline at end of file diff --git a/Vidly/Views/Home/Old_Index.cshtml b/Vidly/Views/Home/Old_Index.cshtml new file mode 100644 index 0000000..32e1dd9 --- /dev/null +++ b/Vidly/Views/Home/Old_Index.cshtml @@ -0,0 +1,31 @@ +@{ + ViewBag.Title = "Home Page"; +} + +
+

ASP.NET

+

ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.

+

Learn more »

+
+ +
+
+

Getting started

+

+ ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that + enables a clean separation of concerns and gives you full control over markup + for enjoyable, agile development. +

+

Learn more »

+
+
+

Get more libraries

+

NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.

+

Learn more »

+
+
+

Web Hosting

+

You can easily find a web hosting company that offers the right mix of features and price for your applications.

+

Learn more »

+
+
\ No newline at end of file diff --git a/Vidly/Views/Movies/Movies.cshtml b/Vidly/Views/Movies/Index.cshtml similarity index 100% rename from Vidly/Views/Movies/Movies.cshtml rename to Vidly/Views/Movies/Index.cshtml diff --git a/Vidly/Views/Shared/_NavBar.cshtml b/Vidly/Views/Shared/_NavBar.cshtml index 70b22b3..3ada024 100644 --- a/Vidly/Views/Shared/_NavBar.cshtml +++ b/Vidly/Views/Shared/_NavBar.cshtml @@ -4,12 +4,12 @@ - @Html.ActionLink("Vidly", "Index", "Customers", new { area = "" }, new { @class = "navbar-brand" }) + @Html.ActionLink("Vidly", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })