2. Na Visual Studio Code si nainštalujeme C# extension
3. Vytvoríme si nový prečinok, kde bude náš nový projekt
4. Vytvoríme si nový projekt pomocou príkazu:
dotnet new mvc -o NewProjectName
dotnet dev-certs https --trust
1. Vytvoríme nový súbor v priečinku /Models s názvom Movie.cs
2. Do súboru vložíme:
using System;
using System.ComponentModel.DataAnnotations;
namespace MvcMovie.Models
{
public class Movie
{
public int Id { get; set; }
public string Name { get; set; }
}
}
1. Vytvoríme nový súbor v priečinku /Controllers s názvom MoviesController.cs
2. Do súboru vložíme:
using Microsoft.AspNetCore.Mvc;
using System.Text.Encodings.Web;
namespace MvcMovie.Controllers
{
public class MoviesController : Controller
{
//
// GET: /Movies/
public string Index()
{
return "Hello World ...";
}
// GET: /Movies/Welcome/
public string Welcome()
{
return "This is the action method ...";
}
}
}
using Microsoft.AspNetCore.Mvc;
using System.Text.Encodings.Web;
namespace MvcMovie.Controllers
{
public class MoviesController : Controller
{
//
// GET: /Movies/
public string Index()
{
return "Hello World ...";
}
//
// GET: /Movies/Welcome/
public string Welcome(string name, int id = 1)
{
return HtmlEncoder.Default.Encode($"Hello {name}, ID: {id}");
}
}
}
using Microsoft.AspNetCore.Mvc;
using System.Text.Encodings.Web;
using MvcMovie.Models;
namespace MvcMovie.Controllers
{
public class MoviesController : Controller
{
//
// GET: /Movies/
public ActionResult Index()
{
var movie = new Movie() { Name = "Shrek!"};
return View();
}
}
}
1. V priečinku /Views si vytvoríme nový prečinok /Movies
2. V priečinku /Views/Movies si vytvoríme nový súbor Index.cshtml
3. Do súboru vložíme:
@{
ViewData["Title"] = "Index";
}
<h2>Hello</h2>
<p>This is new view for movies.</p>
@model MvcMovie.Models.Movie
@{
ViewData["Title"] = "Index";
}
<h2>Movies</h2>
<p>@Model.Name</p>
1. Vytvoríme nový model Customer
2. Vytvoríme nový model RandomMovieViewModel
3. Zmeníme MoviesController na:
using Microsoft.AspNetCore.Mvc;
using System.Text.Encodings.Web;
using MvcMovie.Models;
using System.Collections.Generic;
namespace MvcMovie.Controllers
{
public class MoviesController : Controller
{
//
// GET: /Movies/
public ActionResult Index()
{
var movie = new Movie() { Name = "Shrek!"};
var customers = new List
}
@model MvcMovies.Models.RandomMovieViewModel
@{
ViewData["Title"] = "Index";
}
<h2>@Model.Movie.Name</h2>
@if (Model.Customers.Count == 0){
<text>No one has rented this movie before.</text>
}
<li>@customer.Name</li>
}
</ul>
}
using Microsoft.AspNetCore.Mvc;
using System.Text.Encodings.Web;
using MvcMovie.Models;
using System.Collections.Generic;
using System.Linq;
namespace MvcMovie.Controllers
{
public class CustomersController : Controller
{
public ViewResult Index(){
var customers = GetCustomers();
return View(customers);
}
public ActionResult Details(int id)
{
var customer = GetCustomers().SingleOrDefault(c => c.Id == id);
return View(customer);
}
private IEnumerable<Customer>
}
using Microsoft.AspNetCore.Mvc;
using System.Text.Encodings.Web;
using MvcMovie.Models;
using System.Collections.Generic;
namespace MvcMovie.Controllers
{
public class MoviesController : Controller
{
//
// GET: /Movies/
public ViewResult Index()
{
var movies = GetMovies();
return View(movies);
}
private IEnumerable<Customer> GetMovies()
}
7. Vytvoríme nový priečinok /Views/Customers/
8. V tomto priečinku si vytvoríme súbor Details.cshtml
@model MvcMovie.Models.Customer
@{
ViewBag.Title = Model.Name;
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>@Model.Name</h2>
@model IEnumerable<MvcMovie.Models.Customer>
@{
ViewBag.Title = "Customers";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@if (!Model.Any())
{
}
else
{
<table class="table table-bordered table-hover">
<thead></table>
}
@model IEnumerable<MvcMovie.Models.Movie>
@{
ViewBag.Title = "Movies";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<th>Movie</th>
<tbody>
@foreach (var movie in Model)
{
<tr>
<td>@movie.Name</td>
</tr>
}
</tbody>
1. Pridať niekoľko nových filmov a zákazníkov
2. Pridať zákazníkovi nové parametre a vypísať ich do /Views/Customers/Details.cshtml
3. Pridať filmu nové parametre a vypísať ich do /Views/Movies/Details.cshtml