Lite upprensning bland kontroller och views, cutomers kan läggas till och uppdateras

This commit is contained in:
2019-01-22 22:21:05 +01:00
parent 00f1700aa6
commit 11b51493fe
16 changed files with 221 additions and 81 deletions

View File

@ -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 }
);
}
}

View File

@ -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();
//}
}
}

View File

@ -8,6 +8,10 @@ namespace Vidly.Controllers
{
public class HomeController : Controller
{
[Route("")]
[Route("Home")]
[Route("Home/Index")]
public ActionResult Index()
{
return View();

View File

@ -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()
{

View File

@ -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; }
}
}

View File

@ -22,6 +22,7 @@ namespace Vidly.Models
{
public DbSet<Customer> Customers { get; set; }
public DbSet<Movie> Movies { get; set; }
public DbSet<MembershipType> MembershipTypes { get; set; }
public ApplicationDbContext()
// : base("DefaultConnection", throwIfV1Schema: false)

View File

@ -241,6 +241,7 @@
<Compile Include="Startup.cs" />
<Compile Include="ViewModels\CustomersViewModel.cs" />
<Compile Include="ViewModels\MoviesViewModel.cs" />
<Compile Include="ViewModels\CustomerFormViewModel.cs" />
<Compile Include="ViewModels\RandomMovieViewModel.cs" />
</ItemGroup>
<ItemGroup>
@ -278,7 +279,7 @@
<Content Include="Views\Shared\_Layout.cshtml" />
<Content Include="Views\Home\About.cshtml" />
<Content Include="Views\Home\Contact.cshtml" />
<Content Include="Views\Home\Index.cshtml" />
<Content Include="Views\Home\Old_Index.cshtml" />
<Content Include="Views\Account\_ExternalLoginsListPartial.cshtml" />
<Content Include="Views\Account\ConfirmEmail.cshtml" />
<Content Include="Views\Account\ExternalLoginConfirmation.cshtml" />
@ -301,11 +302,13 @@
<Content Include="Views\Shared\_LoginPartial.cshtml" />
<Content Include="Views\Movies\Random.cshtml" />
<Content Include="Views\Shared\_NavBar.cshtml" />
<Content Include="Views\Customers\Old_Index.cshtml" />
<Content Include="Views\Customers\Index.cshtml" />
<Content Include="Views\Customers\Customers.cshtml" />
<Content Include="Views\Movies\Movies.cshtml" />
<Content Include="Views\Movies\Index.cshtml" />
<Content Include="Views\Customers\Customer.cshtml" />
<Content Include="Views\Movies\Movie.cshtml" />
<Content Include="Views\Customers\CustomerForm.cshtml" />
<Content Include="Views\Home\Index.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />

View File

@ -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<MembershipType> MembershipTypes { get; set; }
public Customer Customer { get; set; }
}
}

View File

@ -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)
{
<h2>New Customer</h2>
}
else
{
<h2>Edit Customer</h2>
}
@using (@Html.BeginForm("Save", "Customers"))
{
<div class="form-group">
@Html.LabelFor(m => m.Customer.Name)
@Html.TextBoxFor(m => m.Customer.Name, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.Customer.BirthDate)
@Html.TextBoxFor(m => m.Customer.BirthDate, "{0:d MMM yyyy}", new { @class = "form-control" })
</div>
<div class="form-group">
@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" })
</div>
<div class="checkbox">
<label>
@Html.CheckBoxFor(m => m.Customer.IsSubscribedToNewsLetter) Subscribed to Newsletter ?
</label>
</div>
@Html.HiddenFor(m => m.Customer.Id)
<button type="submit" class="btn btn-primary">Save</button>
}

View File

@ -1,33 +0,0 @@
@model Vidly.ViewModels.CustomersViewModel
@{
ViewBag.Title = "Customers";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Customers</h2>
@if (Model.Customers.Count == 0)
{
<text>No one has rented this movie before.</text>
}
else
{
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Customer</th>
<th >Membership Type</th>
</tr>
</thead>
<tbody>
@foreach (var customer in Model.Customers)
{
<tr>
<td>@Html.ActionLink(@customer.Name, "Details", "Customers", new { id = @customer.Id }, null)</td>
<td>@customer.MembershipType.Name</td>
</tr>
}
</tbody>
</table>
}

View File

@ -1,11 +1,37 @@

@model Vidly.ViewModels.CustomersViewModel
@{
ViewBag.Title = "Vidly";
ViewBag.Title = "Customers";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="jumbotron">
<h1>V i d l y</h1>
<p class="lead">Vidly is a great video rental system built in MVC5 with entity framework, Razorviews and Javascript.</p>
@*<p><a href="https://asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>*@
</div>
<h2>Customers</h2>
@*<button type="submit" class="btn btn-primary" onclick="">New Customer</button>*@
@Html.ActionLink("New Customer", "New", "Customers", null, new { @class = "btn btn-primary" })
@if (Model.Customers.Count == 0)
{
<text>No one has rented this movie before.</text>
}
else
{
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Customer</th>
<th>Membership Type</th>
</tr>
</thead>
<tbody>
@foreach (var customer in Model.Customers)
{
<tr>
<td>@Html.ActionLink(@customer.Name, "Edit", "Customers", new { id = @customer.Id }, null)</td>
<td>@customer.MembershipType.Name</td>
</tr>
}
</tbody>
</table>
}

View File

@ -0,0 +1,11 @@

@{
ViewBag.Title = "Vidly";
}
<div class="jumbotron">
<h1>V i d l y</h1>
<p class="lead">Vidly is a great video rental system built in MVC5 with entity framework, Razorviews and Javascript.</p>
@*<p><a href="https://asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>*@
</div>

View File

@ -1,31 +1,11 @@
@{
ViewBag.Title = "Home Page";

@{
ViewBag.Title = "Vidly";
}
<div class="jumbotron">
<h1>ASP.NET</h1>
<p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
<p><a href="https://asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>
<h1>V i d l y</h1>
<p class="lead">Vidly is a great video rental system built in MVC5 with entity framework, Razorviews and Javascript.</p>
@*<p><a href="https://asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>*@
</div>
<div class="row">
<div class="col-md-4">
<h2>Getting started</h2>
<p>
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.
</p>
<p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301865">Learn more &raquo;</a></p>
</div>
<div class="col-md-4">
<h2>Get more libraries</h2>
<p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p>
<p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301866">Learn more &raquo;</a></p>
</div>
<div class="col-md-4">
<h2>Web Hosting</h2>
<p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p>
<p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301867">Learn more &raquo;</a></p>
</div>
</div>

View File

@ -0,0 +1,31 @@
@{
ViewBag.Title = "Home Page";
}
<div class="jumbotron">
<h1>ASP.NET</h1>
<p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
<p><a href="https://asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>
</div>
<div class="row">
<div class="col-md-4">
<h2>Getting started</h2>
<p>
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.
</p>
<p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301865">Learn more &raquo;</a></p>
</div>
<div class="col-md-4">
<h2>Get more libraries</h2>
<p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p>
<p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301866">Learn more &raquo;</a></p>
</div>
<div class="col-md-4">
<h2>Web Hosting</h2>
<p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p>
<p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301867">Learn more &raquo;</a></p>
</div>
</div>

View File

@ -4,12 +4,12 @@
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
@Html.ActionLink("Vidly", "Index", "Customers", new { area = "" }, new { @class = "navbar-brand" })
@Html.ActionLink("Vidly", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
<div class="navbar-collapse collapse" id="navbarSupportedContent">
<ul class="nav navbar-nav mr-auto">
<li class="nav-item">@Html.ActionLink("Customers", "Customers", "Customers", null, new { @class = "nav-link" })</li>
<li class="nav-item">@Html.ActionLink("Movies", "Movies", "Movies", null, new { @class = "nav-link" })</li>
<li class="nav-item">@Html.ActionLink("Customers", "Index", "Customers", null, new { @class = "nav-link" })</li>
<li class="nav-item">@Html.ActionLink("Movies", "Index", "Movies", null, new { @class = "nav-link" })</li>
@*<li class="nav-item">@Html.ActionLink("Contact", "Contact", "Home", null, new { @class = "nav-link" })</li>*@
</ul>
@Html.Partial("_LoginPartial")