Skip to content

Commit

Permalink
Added the step-by-step tutorials
Browse files Browse the repository at this point in the history
  • Loading branch information
NORTHAMERICA\mnaggaga committed Apr 2, 2018
1 parent b3f68d5 commit 6c20406
Show file tree
Hide file tree
Showing 170 changed files with 49,007 additions and 8 deletions.
64 changes: 63 additions & 1 deletion Tutorial/2-Add a model/Addamodel.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,66 @@ Open Startup.cs file and add the code below to the ConfigureServices method.
services.AddMvc();
}
```
Add the following using statements `using RazorPagesMovie.Models` and `using Microsoft.EntityFrameworkCore`.
Add the following using statements `using RazorPagesMovie.Models` and `using Microsoft.EntityFrameworkCore`.

#### Add the Entity Framework tools

- Install the `Microsoft.EntityFrameworkCore.Tools.DotNet` package to `DotNetCliToolReference` in RazorPagesMovie.csproj.

```
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.2" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.2" />
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1" />
</ItemGroup>
</Project>
```
#### Add scaffold tooling and perform initial migration

In the command line run the following commands
```
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet restore
dotnet ef migrations add InitialCreate
dotnet ef database update
```
Commands Explained
| Command | Description |
| ------------- |:-------------:|
| ` add package` | installs the tools needed |
| `ef migrations add InitialCreate` | generates code to create the initial database schema based on the model specified in 'MovieContenxt.cs'.`InitialCreate` is the name of the migrations. |
|`ef database update` | creates the database |

#### Scaffold the movie model

Run the commands below

*On Windows*

`dotnet aspnet-codegenerator razorpage -m Movie -dc MovieContext -udl -outDir Pages\Movies --referenceScriptLibraries`

*On Mac and Linux*

`dotnet aspnet-codegenerator razorpage -m Movie -dc MovieContext -udl -outDir Pages/Movies --referenceScriptLibraries`
#### Test your app
- Run the app `dotnet run`
- Launch a browser and go to `http://localhost:5000/movies`

![](images\moviespage.PNG)
- create a new entry

![](images\createnew.PNG)

- It works!
![](images\newentry.PNG.PNG)
- Test to se Edit, Details and delete

To learn more on the pages created in this tutorial read [this post](https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages-vsc/page)

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Collections.Generic;

namespace RazorPagesMovie.Migrations
{
public partial class InitialCreate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Movie",
columns: table => new
{
ID = table.Column<int>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Genre = table.Column<string>(nullable: true),
Price = table.Column<decimal>(nullable: false),
ReleaseDate = table.Column<DateTime>(nullable: false),
Title = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Movie", x => x.ID);
});
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Movie");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.Internal;
using RazorPagesMovie.Models;
using System;

namespace RazorPagesMovie.Migrations
{
[DbContext(typeof(MovieContext))]
partial class MovieContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.0.1-rtm-125");

modelBuilder.Entity("RazorPagesMovie.Models.Movie", b =>
{
b.Property<int>("ID")
.ValueGeneratedOnAdd();

b.Property<string>("Genre");

b.Property<decimal>("Price");

b.Property<DateTime>("ReleaseDate");

b.Property<string>("Title");

b.HasKey("ID");

b.ToTable("Movie");
});
#pragma warning restore 612, 618
}
}
}
Binary file not shown.
49 changes: 49 additions & 0 deletions Tutorial/2-Add a model/RazorPagesMovie/Pages/Movies/Create.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
@page
@model RazorPagesMovie.Pages.Movies.CreateModel

@{
ViewData["Title"] = "Create";
}

<h2>Create</h2>

<h4>Movie</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Movie.Title" class="control-label"></label>
<input asp-for="Movie.Title" class="form-control" />
<span asp-validation-for="Movie.Title" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Movie.ReleaseDate" class="control-label"></label>
<input asp-for="Movie.ReleaseDate" class="form-control" />
<span asp-validation-for="Movie.ReleaseDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Movie.Genre" class="control-label"></label>
<input asp-for="Movie.Genre" class="form-control" />
<span asp-validation-for="Movie.Genre" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Movie.Price" class="control-label"></label>
<input asp-for="Movie.Price" class="form-control" />
<span asp-validation-for="Movie.Price" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>
</div>
</div>

<div>
<a asp-page="Index">Back to List</a>
</div>

@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using RazorPagesMovie.Models;

namespace RazorPagesMovie.Pages.Movies
{
public class CreateModel : PageModel
{
private readonly RazorPagesMovie.Models.MovieContext _context;

public CreateModel(RazorPagesMovie.Models.MovieContext context)
{
_context = context;
}

public IActionResult OnGet()
{
return Page();
}

[BindProperty]
public Movie Movie { get; set; }

public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}

_context.Movie.Add(Movie);
await _context.SaveChangesAsync();

return RedirectToPage("./Index");
}
}
}
46 changes: 46 additions & 0 deletions Tutorial/2-Add a model/RazorPagesMovie/Pages/Movies/Delete.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
@page
@model RazorPagesMovie.Pages.Movies.DeleteModel

@{
ViewData["Title"] = "Delete";
}

<h2>Delete</h2>

<h3>Are you sure you want to delete this?</h3>
<div>
<h4>Movie</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Movie.Title)
</dt>
<dd>
@Html.DisplayFor(model => model.Movie.Title)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Movie.ReleaseDate)
</dt>
<dd>
@Html.DisplayFor(model => model.Movie.ReleaseDate)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Movie.Genre)
</dt>
<dd>
@Html.DisplayFor(model => model.Movie.Genre)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Movie.Price)
</dt>
<dd>
@Html.DisplayFor(model => model.Movie.Price)
</dd>
</dl>

<form method="post">
<input type="hidden" asp-for="Movie.ID" />
<input type="submit" value="Delete" class="btn btn-default" /> |
<a asp-page="./Index">Back to List</a>
</form>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using RazorPagesMovie.Models;

namespace RazorPagesMovie.Pages.Movies
{
public class DeleteModel : PageModel
{
private readonly RazorPagesMovie.Models.MovieContext _context;

public DeleteModel(RazorPagesMovie.Models.MovieContext context)
{
_context = context;
}

[BindProperty]
public Movie Movie { get; set; }

public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}

Movie = await _context.Movie.SingleOrDefaultAsync(m => m.ID == id);

if (Movie == null)
{
return NotFound();
}
return Page();
}

public async Task<IActionResult> OnPostAsync(int? id)
{
if (id == null)
{
return NotFound();
}

Movie = await _context.Movie.FindAsync(id);

if (Movie != null)
{
_context.Movie.Remove(Movie);
await _context.SaveChangesAsync();
}

return RedirectToPage("./Index");
}
}
}
Loading

0 comments on commit 6c20406

Please sign in to comment.