Min lösning på övningen
This commit is contained in:
@ -24,7 +24,7 @@ namespace Vidly
|
||||
routes.MapRoute(
|
||||
name: "Default",
|
||||
url: "{controller}/{action}/{id}",
|
||||
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
|
||||
defaults: new { controller = "Customers", action = "Index", id = UrlParameter.Optional }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
body {
|
||||
padding-top: 50px;
|
||||
padding-top: 70px;
|
||||
padding-bottom: 20px;
|
||||
}
|
||||
|
||||
|
||||
60
Vidly/Controllers/CustomersController.cs
Normal file
60
Vidly/Controllers/CustomersController.cs
Normal file
@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Vidly.Models;
|
||||
using Vidly.ViewModels;
|
||||
|
||||
namespace Vidly.Controllers
|
||||
{
|
||||
public class CustomersController : Controller
|
||||
{
|
||||
List<Customer> customers = null;
|
||||
|
||||
public CustomersController()
|
||||
{
|
||||
customers = new List<Customer>
|
||||
{
|
||||
new Customer {Name="Nils Persson", Id=1},
|
||||
new Customer {Name="Pelle Rundström",Id=2},
|
||||
new Customer {Name="Ulla Ulriksson",Id=3}
|
||||
};
|
||||
}
|
||||
|
||||
// GET: Vidly
|
||||
public ActionResult Index()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
public ActionResult Customers()
|
||||
{
|
||||
|
||||
var viewModel = new CustomersViewModel()
|
||||
{
|
||||
Customers = customers
|
||||
};
|
||||
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
[Route("Customers/Details/{nr}")]
|
||||
public ActionResult Customer(int nr)
|
||||
{
|
||||
foreach(var cust in customers)
|
||||
{
|
||||
if (cust.Id == nr)
|
||||
{
|
||||
return View(cust);
|
||||
}
|
||||
}
|
||||
return View(new Customer { Name="",Id=0});
|
||||
}
|
||||
|
||||
public ActionResult Movies()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -10,6 +10,16 @@ namespace Vidly.Controllers
|
||||
{
|
||||
public class MoviesController : Controller
|
||||
{
|
||||
List<Movie> movies = null;
|
||||
public MoviesController()
|
||||
{
|
||||
movies = new List<Movie>
|
||||
{
|
||||
new Movie{Name="Never say never again", Id=1},
|
||||
new Movie{Name="Doctor Zivago",Id=2},
|
||||
new Movie{Name="Hånkentomtarna",Id=3}
|
||||
};
|
||||
}
|
||||
// GET: Movies
|
||||
public ActionResult Random()
|
||||
{
|
||||
@ -22,7 +32,8 @@ namespace Vidly.Controllers
|
||||
//var viewResult = new ViewResult();
|
||||
//viewResult.ViewData.Model = movie;
|
||||
|
||||
var viewModel = new RandomMovieViewModel() {
|
||||
var viewModel = new RandomMovieViewModel()
|
||||
{
|
||||
Movie = movie,
|
||||
Customers = customers
|
||||
};
|
||||
@ -54,11 +65,22 @@ namespace Vidly.Controllers
|
||||
return Content($"pageIndex={pageIndex}&sortBy={sortBy}");
|
||||
}
|
||||
|
||||
public ActionResult Movies()
|
||||
{
|
||||
var viewModel = new MoviesViewModel()
|
||||
{
|
||||
Movies = movies
|
||||
};
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
[Route("movies/released/{year}/{month:regex(\\d{2}):range(1,12)}")]
|
||||
public ActionResult ByReleaseDate(int year, int month)
|
||||
{
|
||||
return Content(year + "/" + month);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@ -177,6 +177,7 @@
|
||||
<Compile Include="Controllers\HomeController.cs" />
|
||||
<Compile Include="Controllers\ManageController.cs" />
|
||||
<Compile Include="Controllers\MoviesController.cs" />
|
||||
<Compile Include="Controllers\CustomersController.cs" />
|
||||
<Compile Include="Global.asax.cs">
|
||||
<DependentUpon>Global.asax</DependentUpon>
|
||||
</Compile>
|
||||
@ -187,6 +188,8 @@
|
||||
<Compile Include="Models\Movie.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Startup.cs" />
|
||||
<Compile Include="ViewModels\CustomersViewModel.cs" />
|
||||
<Compile Include="ViewModels\MoviesViewModel.cs" />
|
||||
<Compile Include="ViewModels\RandomMovieViewModel.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
@ -247,6 +250,10 @@
|
||||
<Content Include="Views\Shared\_LoginPartial.cshtml" />
|
||||
<Content Include="Views\Movies\Random.cshtml" />
|
||||
<Content Include="Views\Shared\_NavBar.cshtml" />
|
||||
<Content Include="Views\Customers\Index.cshtml" />
|
||||
<Content Include="Views\Customers\Customers.cshtml" />
|
||||
<Content Include="Views\Movies\Movies.cshtml" />
|
||||
<Content Include="Views\Customers\Customer.cshtml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="App_Data\" />
|
||||
|
||||
13
Vidly/ViewModels/CustomersViewModel.cs
Normal file
13
Vidly/ViewModels/CustomersViewModel.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using Vidly.Models;
|
||||
|
||||
namespace Vidly.ViewModels
|
||||
{
|
||||
public class CustomersViewModel
|
||||
{
|
||||
public List<Customer> Customers { get; set; }
|
||||
}
|
||||
}
|
||||
13
Vidly/ViewModels/MoviesViewModel.cs
Normal file
13
Vidly/ViewModels/MoviesViewModel.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using Vidly.Models;
|
||||
|
||||
namespace Vidly.ViewModels
|
||||
{
|
||||
public class MoviesViewModel
|
||||
{
|
||||
public List<Movie> Movies { get; set; }
|
||||
}
|
||||
}
|
||||
10
Vidly/Views/Customers/Customer.cshtml
Normal file
10
Vidly/Views/Customers/Customer.cshtml
Normal file
@ -0,0 +1,10 @@
|
||||
@model Vidly.Models.Customer
|
||||
@{
|
||||
ViewBag.Title = "Customer";
|
||||
Layout = "~/Views/Shared/_Layout.cshtml";
|
||||
}
|
||||
|
||||
<h2>Customer</h2>
|
||||
|
||||
<h1>@Model.Name - @Model.Id</h1>
|
||||
|
||||
23
Vidly/Views/Customers/Customers.cshtml
Normal file
23
Vidly/Views/Customers/Customers.cshtml
Normal file
@ -0,0 +1,23 @@
|
||||
@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
|
||||
{
|
||||
<ul>
|
||||
@foreach (var customer in Model.Customers)
|
||||
{
|
||||
@*<li>@customer.Name</li>*@
|
||||
<li>@Html.ActionLink(@customer.Name, "Details", "Customers", new { id = @customer.Id },null)</li>
|
||||
}
|
||||
</ul>
|
||||
|
||||
}
|
||||
11
Vidly/Views/Customers/Index.cshtml
Normal file
11
Vidly/Views/Customers/Index.cshtml
Normal 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 »</a></p>*@
|
||||
</div>
|
||||
|
||||
23
Vidly/Views/Movies/Movies.cshtml
Normal file
23
Vidly/Views/Movies/Movies.cshtml
Normal file
@ -0,0 +1,23 @@
|
||||
@model Vidly.ViewModels.MoviesViewModel
|
||||
@{
|
||||
ViewBag.Title = "Movies";
|
||||
Layout = "~/Views/Shared/_Layout.cshtml";
|
||||
}
|
||||
|
||||
<h2>Movies</h2>
|
||||
|
||||
@if (Model.Movies.Count == 0)
|
||||
{
|
||||
<text>No one has rented this movie before.</text>
|
||||
}
|
||||
else
|
||||
{
|
||||
<ul>
|
||||
@foreach (var movie in Model.Movies)
|
||||
{
|
||||
<li>@movie.Name</li>
|
||||
@*<li>@Html.ActionLink(@customer.Name, "Details", "Customers", new { id = @customer.Id }, null)</li>*@
|
||||
}
|
||||
</ul>
|
||||
|
||||
}
|
||||
@ -4,13 +4,13 @@
|
||||
<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("My Web Portal", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
|
||||
@Html.ActionLink("Vidly", "Index", "Customers", 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("Home", "Index", "Home", null, new { @class = "nav-link" })</li>
|
||||
<li class="nav-item">@Html.ActionLink("About", "About", "Home", null, new { @class = "nav-link" })</li>
|
||||
<li class="nav-item">@Html.ActionLink("Contact", "Contact", "Home", null, new { @class = "nav-link" })</li>
|
||||
<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("Contact", "Contact", "Home", null, new { @class = "nav-link" })</li>*@
|
||||
</ul>
|
||||
@Html.Partial("_LoginPartial")
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user