commit
09b342b45e
|
@ -165,7 +165,7 @@ _context.ChangeTracker.AutoDetectChangesEnabled = false;
|
|||
|
||||
## Entity Framework Core source code and development plans
|
||||
|
||||
The Entity Framework Core source is at [https://github.com/aspnet/EntityFrameworkCore](https://github.com/aspnet/EntityFrameworkCore). The EF Core repository contains nightly builds, issue tracking, feature specs, design meeting notes, and the [the roadmap for future development](https://github.com/aspnet/EntityFrameworkCore/wiki/Roadmap). You can file or find bugs, and contribute.
|
||||
The Entity Framework Core source is at [https://github.com/aspnet/EntityFrameworkCore](https://github.com/aspnet/EntityFrameworkCore). The EF Core repository contains nightly builds, issue tracking, feature specs, design meeting notes, and [the roadmap for future development](https://github.com/aspnet/EntityFrameworkCore/wiki/Roadmap). You can file or find bugs, and contribute.
|
||||
|
||||
Although the source code is open, Entity Framework Core is fully supported as a Microsoft product. The Microsoft Entity Framework team keeps control over which contributions are accepted and tests all code changes to ensure the quality of each release.
|
||||
|
||||
|
|
|
@ -256,7 +256,7 @@ For *appsettings* files where:
|
|||
|
||||
If all the preceding conditions are true, the command-line arguments are overridden.
|
||||
|
||||
ASP.NET Core 2.x app can use WebHostBuilder](/dotnet/api/microsoft.aspnetcore.hosting.webhostbuilder) instead of ``CreateDefaultBuilder`. When using `WebHostBuilder`, manually set configuration with [ConfigurationBuilder](/api/microsoft.extensions.configuration.configurationbuilder). See the ASP.NET Core 1.x tab for more information.
|
||||
ASP.NET Core 2.x app can use [WebHostBuilder](/dotnet/api/microsoft.aspnetcore.hosting.webhostbuilder) instead of `CreateDefaultBuilder`. When using `WebHostBuilder`, manually set configuration with [ConfigurationBuilder](/api/microsoft.extensions.configuration.configurationbuilder). See the ASP.NET Core 1.x tab for more information.
|
||||
|
||||
# [ASP.NET Core 1.x](#tab/aspnetcore1x)
|
||||
|
||||
|
|
|
@ -0,0 +1,138 @@
|
|||
# Scaffolded Razor Pages in ASP.NET Core
|
||||
|
||||
By [Rick Anderson](https://twitter.com/RickAndMSFT)
|
||||
|
||||
This tutorial examines the Razor Pages created by scaffolding in the previous tutorial.
|
||||
|
||||
[View or download](https://github.com/aspnet/Docs/tree/master/aspnetcore/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie) sample.
|
||||
|
||||
## The Create, Delete, Details, and Edit pages.
|
||||
|
||||
Examine the *Pages/Movies/Index.cshtml.cs* Page Model:
|
||||
[!code-csharp[Main](../../tutorials/razor-pages/razor-pages-start/snapshot_sample/RazorPagesMovie/Pages/Movies/Index.cshtml.cs)]
|
||||
|
||||
Razor Pages are derived from `PageModel`. By convention, the `PageModel`-derived class is called `<PageName>Model`. The constructor uses [dependency injection](xref:fundamentals/dependency-injection) to add the `MovieContext` to the page. All the scaffolded pages follow this pattern. See [Asynchronous code](xref:data/ef-rp/intro#asynchronous-code) for more information on asynchronous programing with Entity Framework.
|
||||
|
||||
When a request is made for the page, the `OnGetAsync` method returns a list of movies to the Razor Page. `OnGetAsync` or `OnGet` is called on a Razor Page to initialize the state for the page. In this case, `OnGetAsync` gets a list of movies and displays them.
|
||||
|
||||
When `OnGet` returns `void` or `OnGetAsync` returns`Task`, no return method is used. When the return type is `IActionResult` or `Task<IActionResult>`, a return statement must be provided. For example, the *Pages/Movies/Create.cshtml.cs* `OnPostAsync` method:
|
||||
|
||||
<!-- TODO - replace with snippet
|
||||
[!code-csharp[Main](../../tutorials/razor-pages/razor-pages-start/snapshot_sample/RazorPagesMovie/Pages/Movies/Create.cshtml.cs?name=snippetALL)]
|
||||
-->
|
||||
|
||||
```csharp
|
||||
public async Task<IActionResult> OnPostAsync()
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return Page();
|
||||
}
|
||||
|
||||
_context.Movie.Add(Movie);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return RedirectToPage("./Index");
|
||||
}
|
||||
```
|
||||
Examine the *Pages/Movies/Index.cshtml* Razor Page:
|
||||
|
||||
[!code-cshtml[Main](../../tutorials/razor-pages/razor-pages-start/snapshot_sample/RazorPagesMovie/Pages/Movies/Index.cshtml)]
|
||||
|
||||
Razor can transition from HTML into C# or into Razor-specific markup. When an `@` symbol is followed by a [Razor reserved keyword](xref:mvc/views/razor#razor-reserved-keywords), it transitions into Razor-specific markup, otherwise it transitions into C#.
|
||||
|
||||
The `@page` Razor directive makes the file into an MVC action — which means that it can handle requests. `@page` must be the first Razor directive on a page. `@page` is an example of transitioning into Razor-specific markup. See [Razor syntax](xref:mvc/views/razor#razor-syntax) for more information.
|
||||
|
||||
Examine the lambda expression used in the following HTML Helper:
|
||||
|
||||
```cshtml
|
||||
@Html.DisplayNameFor(model => model.Movie[0].Title))
|
||||
```
|
||||
|
||||
The `DisplayNameFor` HTML Helper inspects the `Title` property referenced in the lambda expression to determine the display name. The lambda expression is inspected rather than evaluated. That means there is no access violation when `model`, `model.Movie`, or `model.Movie[0]` are `null` or empty. When the lambda expression is evaluated (for example, with `@Html.DisplayFor(modelItem => item.Title)`), the model's property values are evaluated.
|
||||
|
||||
<a name="md"></a>
|
||||
### The @model directive
|
||||
|
||||
[!code-cshtml[Main](../../tutorials/razor-pages/razor-pages-start/snapshot_sample/RazorPagesMovie/Pages/Movies/Index.cshtml?range=1-2&highlight=2)]
|
||||
|
||||
The `@model` directive specifies the type of the model passed to the Razor Page. In the preceding example, the `@model` line makes the `PageModel`-derived class available to the Razor Page. The model is used in the `@Html.DisplayNameFor` and `@Html.DisplayName` [HTML Helpers](https://docs.microsoft.com/aspnet/mvc/overview/older-versions-1/views/creating-custom-html-helpers-cs#understanding-html-helpers) on the page.
|
||||
|
||||
<!-- why don't xref links work?
|
||||
[HTML Helpers 2](xref:aspnet/mvc/overview/older-versions-1/views/creating-custom-html-helpers-cs)
|
||||
-->
|
||||
|
||||
<a name="vd"></a>
|
||||
### ViewData and layout
|
||||
|
||||
Consider the following code:
|
||||
|
||||
[!code-cshtml[Main](../../tutorials/razor-pages/razor-pages-start/snapshot_sample/RazorPagesMovie/Pages/Movies/Index.cshtml?range=1-6&highlight=4-)]
|
||||
|
||||
The preceding highlighted code is an example of Razor transitioning into C#. The `{` and `}` characters enclose a block of C# code.
|
||||
|
||||
The `PageModel` base class has a `ViewData` dictionary property that can be used to add data that you want to pass to a View. You add objects into the `ViewData` dictionary using a key/value pattern. In the preceding sample, the "Title" property is added to the `ViewData` dictionary. The "Title" property is used in the *Pages/_Layout.cshtml* file. The following markup shows the first few lines of the *Pages/_Layout.cshtml* file.
|
||||
|
||||
[!code-cshtml[Main](../../tutorials/razor-pages/razor-pages-start/snapshot_sample/RazorPagesMovie/Pages/NU/_Layout1.cshtml?highlight=6-)]
|
||||
|
||||
The line `@*Markup removed for brevity.*@` is a Razor comment. Unlike HTML comments (`<!-- -->`), Razor comments are not sent to the client.
|
||||
|
||||
Run the app and test the links in the project (**Home**, **About**, **Contact**, **Create**, **Edit**, and **Delete**). Each page sets the title, which you can see in the browser tab. When you bookmark a page, the title is used for the bookmark. *Pages/Index.cshtml* and *Pages/Movies/Index.cshtml* currently have the same title, but you can modify them to have different values.
|
||||
|
||||
The `Layout` property is set in the *Pages/_ViewStart.cshtml* file:
|
||||
|
||||
[!code-cshtml[Main](../../tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie/Pages/_ViewStart.cshtml)]
|
||||
|
||||
The preceding markup sets the layout file to *Pages/_Layout.cshtml* for all Razor files under the *Pages* folder. See [Layout](xref:mvc/razor-pages/index#layout) for more information.
|
||||
|
||||
### Update the layout
|
||||
|
||||
Change the `<title>` element in the *Pages/_Layout.cshtml* file to use a shorter string.
|
||||
|
||||
[!code-cshtml[Main](../../tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie/Pages/_Layout.cshtml?range=1-6&highlight=6)]
|
||||
|
||||
Find the following anchor element in the *Pages/_Layout.cshtml* file.
|
||||
|
||||
```cshtml
|
||||
<a asp-page="/Index" class="navbar-brand">RazorPagesMovie</a>
|
||||
```
|
||||
Replace the preceding element with the following markup.
|
||||
|
||||
```cshtml
|
||||
<a asp-page="/Movies/Index" class="navbar-brand">RpMovie</a>
|
||||
```
|
||||
|
||||
The preceding anchor element is a [Tag Helper](xref:mvc/views/tag-helpers/intro). In this case, it's the [Anchor Tag Helper](xref:mvc/views/tag-helpers/builtin-th/anchor-tag-helper). The `asp-page="/Movies/Index"` Tag Helper attribute and value creates a link to the `/Movies/Index` Razor Page.
|
||||
|
||||
Save your changes, and test the app by clicking on the **RpMovie** link. See the [_Layout.cshtml](https://github.com/aspnet/Docs/blob/master/aspnetcore/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie/Pages/_Layout.cshtml) file in GitHub.
|
||||
|
||||
### The Create code-behind page
|
||||
|
||||
Examine the *Pages/Movies/Create.cshtml.cs* code-behind file:
|
||||
|
||||
[!code-csharp[Main](../../tutorials/razor-pages/razor-pages-start/snapshot_sample/RazorPagesMovie/Pages/Movies/Create.cshtml.cs?name=snippetALL)]
|
||||
|
||||
The `OnGet` method initializes any state needed for the page. The Create page doesn't have any state to initialize. The `Page` method creates a `PageResult` object that renders the *Create.cshtml* page.
|
||||
|
||||
The `Movie` property uses the `[BindProperty]` attribute to opt-in to [model binding](xref:mvc/models/model-binding). When the Create form posts the form values, the ASP.NET Core runtime binds the posted values to the `Movie` model.
|
||||
|
||||
The `OnPostAsync` method is run when the page posts form data:
|
||||
|
||||
[!code-csharp[Main](../../tutorials/razor-pages/razor-pages-start/snapshot_sample/RazorPagesMovie/Pages/Movies/Create.cshtml.cs?name=snippetPost)]
|
||||
|
||||
If there are any model errors, the form is redisplayed, along with any form data posted. Most model errors can be caught on the client-side before the form is posted. An example of a model error is posting a value for the date field that cannot be converted to a date. We'll talk more about client-side validation and model validation later in the tutorial.
|
||||
|
||||
If there are no model errors, the data is saved, and the browser is redirected to the Index page.
|
||||
|
||||
### The Create Razor Page
|
||||
|
||||
Examine the *Pages/Movies/Create.cshtml* Razor Page file:
|
||||
|
||||
[!code-cshtml[Main](../../tutorials/razor-pages/razor-pages-start/snapshot_sample/RazorPagesMovie/Pages/Movies/Create.cshtml)]
|
||||
|
||||
<!--
|
||||
Visual Studio displays the `<form method="post">` tag in a distinctive font used for Tag Helpers. The `<form method="post">` element is a [Form Tag Helper](xref:mvc/views/working-with-forms#the-form-tag-helper). The Form Tag Helper automatically includes an [antiforgery token](xref:security/anti-request-forgery).
|
||||
|
||||
|
||||
![VS17 view of Create.cshtml page](page/_static/th.png)
|
||||
-->
|
|
@ -0,0 +1,11 @@
|
|||
The `<form method="post">` element is a [Form Tag Helper](xref:mvc/views/working-with-forms#the-form-tag-helper). The Form Tag Helper automatically includes an [antiforgery token](xref:security/anti-request-forgery).
|
||||
|
||||
The scaffolding engine creates Razor markup for each field in the model (except the ID) similar to the following:
|
||||
|
||||
[!code-cshtml[Main](../../tutorials/razor-pages/razor-pages-start/snapshot_sample/RazorPagesMovie/Pages/Movies/Create.cshtml?range=15-20)]
|
||||
|
||||
The [Validation Tag Helpers](xref:mvc/views/working-with-forms#the-validation-tag-helpers) (`<div asp-validation-summary` and ` <span asp-validation-for`) display validation errors. Validation is covered in more detail later in this series.
|
||||
|
||||
The [Label Tag Helper](xref:mvc/views/working-with-forms#the-label-tag-helper) (`<label asp-for="Movie.Title" class="control-label"></label>`) generates the label caption and `for` attribute for the `Title` property.
|
||||
|
||||
The [Input Tag Helper](xref:mvc/views/working-with-forms) (`<input asp-for="Movie.Title" class="form-control" />`) uses the [DataAnnotations](https://docs.microsoft.com/aspnet/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-6) attributes and produces HTML attributes needed for jQuery Validation on the client-side.
|
|
@ -49,7 +49,7 @@ ASP.NET Core introduced a new mechanism for bootstrapping an app. The entry poin
|
|||
|
||||
[!code-csharp[Main](samples/globalasax-sample.cs)]
|
||||
|
||||
This approach couples the application and the server to which it's deployed in a way that interferes with the implementation. In an effort to decouple, [OWIN](http://owin.org/) was introduced to provide a cleaner way to use multiple frameworks together. OWIN provides a pipeline to add only the modules needed. The hosting environment takes a [Startup](xref:fundamentals/startup) function to configure services and the app's request pipeline. `Startup` registers a set of middleware with the application. For each request, the application calls each of the the middleware components with the head pointer of a linked list to an existing set of handlers. Each middleware component can add one or more handlers to the request handling pipeline. This is accomplished by returning a reference to the handler that's the new head of the list. Each handler is responsible for remembering and invoking the next handler in the list. With ASP.NET Core, the entry point to an application is `Startup`, and you no longer have a dependency on *Global.asax*. When using OWIN with .NET Framework, use something like the following as a pipeline:
|
||||
This approach couples the application and the server to which it's deployed in a way that interferes with the implementation. In an effort to decouple, [OWIN](http://owin.org/) was introduced to provide a cleaner way to use multiple frameworks together. OWIN provides a pipeline to add only the modules needed. The hosting environment takes a [Startup](xref:fundamentals/startup) function to configure services and the app's request pipeline. `Startup` registers a set of middleware with the application. For each request, the application calls each of the middleware components with the head pointer of a linked list to an existing set of handlers. Each middleware component can add one or more handlers to the request handling pipeline. This is accomplished by returning a reference to the handler that's the new head of the list. Each handler is responsible for remembering and invoking the next handler in the list. With ASP.NET Core, the entry point to an application is `Startup`, and you no longer have a dependency on *Global.asax*. When using OWIN with .NET Framework, use something like the following as a pipeline:
|
||||
|
||||
[!code-csharp[Main](samples/webapi-owin.cs)]
|
||||
|
||||
|
|
|
@ -68,7 +68,7 @@ The `View` helper method has several overloads. You can optionally specify:
|
|||
```csharp
|
||||
return View("Orders");
|
||||
```
|
||||
* A [model](xref:mvc/models/model-binding) to pass to the the view:
|
||||
* A [model](xref:mvc/models/model-binding) to pass to the view:
|
||||
|
||||
```csharp
|
||||
return View(Orders);
|
||||
|
|
|
@ -57,6 +57,7 @@
|
|||
### [Razor Pages web app on a Mac](xref:tutorials/razor-pages-mac/index)
|
||||
#### [Getting started with Razor Pages](xref:tutorials/razor-pages-mac/razor-pages-start)
|
||||
#### [Adding a model](xref:tutorials/razor-pages-mac/model)
|
||||
#### [Scaffolded Razor Pages](xref:tutorials/razor-pages-mac/page)
|
||||
### [Razor Pages web app with VS Code](xref:tutorials/razor-pages-vsc/index)
|
||||
#### [Getting started with Razor Pages](xref:tutorials/razor-pages-vsc/razor-pages-start)
|
||||
#### [Adding a model](xref:tutorials/razor-pages-vsc/model)
|
||||
|
|
|
@ -20,11 +20,11 @@ This series explains the basics of building a Razor Pages web app with ASP.NET C
|
|||
|
||||
1. [Getting started with Razor Pages on Mac](xref:tutorials/razor-pages-mac/razor-pages-start)
|
||||
1. [Adding a model to a Razor Pages app](xref:tutorials/razor-pages-mac/model)
|
||||
1. [Scaffolded Razor Pages](xref:tutorials/razor-pages-mac/page)
|
||||
|
||||
|
||||
Until the next section is complete, follow the Visual Studio for Windows version.
|
||||
|
||||
1. [Scaffolded Razor Pages](xref:tutorials/razor-pages/page)
|
||||
1. [Working with SQL Server LocalDB](xref:tutorials/razor-pages/sql)
|
||||
1. [Updating the pages](xref:tutorials/razor-pages/da1)
|
||||
1. [Adding search](xref:tutorials/razor-pages/search)
|
||||
|
|
|
@ -38,7 +38,7 @@ Build the project to verify you don't have any errors.
|
|||
|
||||
### Entity Framework Core NuGet packages for migrations
|
||||
|
||||
The EF tools for the command-line interface (CLI) are provided in [Microsoft.EntityFrameworkCore.Tools.DotNet](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Tools.DotNet). To install this package, add it to the `DotNetCliToolReference` collection in the *.csproj* file. **Note:** You have to install this package by editing the *.csproj* file; you can't use the `install-package` command or the package manager GUI.
|
||||
The EF tools for the command-line interface (CLI) are provided in [Microsoft.EntityFrameworkCore.Tools.DotNet](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Tools.DotNet). Click on the [Microsoft.EntityFrameworkCore.Tools.DotNet](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Tools.DotNet) link to get the version number to use. To install this package, add it to the `DotNetCliToolReference` collection in the *.csproj* file. **Note:** You have to install this package by editing the *.csproj* file; you can't use the `install-package` command or the package manager GUI.
|
||||
|
||||
To edit a *.csproj* file:
|
||||
|
||||
|
@ -48,9 +48,11 @@ To edit a *.csproj* file:
|
|||
|
||||
![Edit csproj file](model/csproj.png)
|
||||
|
||||
Add the `Microsoft.EntityFrameworkCore.Tools.DotNet` tool reference to the second **\<ItemGroup>**:
|
||||
Add the `Microsoft.EntityFrameworkCore.Tools.DotNet` tool reference to the second **\<ItemGroup>**.:
|
||||
|
||||
[!code-xml[Main](../../tutorials/razor-pages/razor-pages-start/snapshot_cli_sample/RazorPagesMovie/RazorPagesMovie.cli.csproj?range=12-16&highlight=4)]
|
||||
[!code-xml[Main](../../tutorials/razor-pages/razor-pages-start/snapshot_cli_sample/RazorPagesMovie/RazorPagesMovie.cli.csproj?highlight=10)]
|
||||
|
||||
The version numbers shown in the following code were correct at the time of writing.
|
||||
|
||||
[!INCLUDE[model3](../../includes/RP/model3.md)]
|
||||
[!INCLUDE[model 4x](../../includes/RP/model4x.md)]
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
---
|
||||
title: Scaffolded Razor Pages in ASP.NET Core
|
||||
author: rick-anderson
|
||||
description: Explains the Razor Pages generated by scaffolding on Mac.
|
||||
ms.author: riande
|
||||
manager: wpickett
|
||||
ms.date: 1/27/2018
|
||||
ms.topic: get-started-article
|
||||
ms.technology: aspnet
|
||||
ms.prod: aspnet-core
|
||||
uid: tutorials/razor-pages-mac/page
|
||||
---
|
||||
|
||||
[!INCLUDE[model1](../../includes/RP/page1.md)]
|
||||
|
||||
[!INCLUDE[model1](../../includes/RP/page2.md)]
|
||||
|
||||
The next tutorial explains SQLite and seeding the database.
|
||||
|
||||
>[!div class="step-by-step"]
|
||||
[Previous: Adding a model](xref:tutorials/razor-pages-mac/model)
|
||||
[Next: SQL Server LocalDB](xref:tutorials/razor-pages/sql)
|
|
@ -14,7 +14,7 @@ uid: tutorials/razor-pages-mac/razor-pages-start
|
|||
|
||||
By [Rick Anderson](https://twitter.com/RickAndMSFT)
|
||||
|
||||
This tutorial teaches the basics of building an ASP.NET Core Razor Pages web app. We recommend you complete [Introduction to Razor Pages](xref:mvc/razor-pages/index) before starting this tutorial. Razor Pages is the recommended way to build UI for web applications in ASP.NET Core.
|
||||
This tutorial teaches the basics of building an ASP.NET Core Razor Pages web app. We recommend you review [Introduction to Razor Pages](xref:mvc/razor-pages/index) before starting this tutorial. Razor Pages is the recommended way to build UI for web applications in ASP.NET Core.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
|
|
|
@ -37,7 +37,7 @@ Edit the *RazorPagesMovie.csproj* file:
|
|||
* Select **File** > **Open File**, and then select the *RazorPagesMovie.csproj* file.
|
||||
* Add tool reference for `Microsoft.EntityFrameworkCore.Tools.DotNet` to the second **\<ItemGroup>**:
|
||||
|
||||
[!code-xml[Main](../../tutorials/razor-pages/razor-pages-start/snapshot_cli_sample/RazorPagesMovie/RazorPagesMovie.cli.csproj?range=12-16&highlight=4)]
|
||||
[!code-xml[Main](../../tutorials/razor-pages/razor-pages-start/snapshot_cli_sample/RazorPagesMovie/RazorPagesMovie.cli.csproj)]
|
||||
|
||||
[!INCLUDE[model 3](../../includes/RP/model3.md)]
|
||||
|
||||
|
|
|
@ -12,149 +12,13 @@ uid: tutorials/razor-pages/page
|
|||
---
|
||||
# Scaffolded Razor Pages in ASP.NET Core
|
||||
|
||||
By [Rick Anderson](https://twitter.com/RickAndMSFT)
|
||||
[!INCLUDE[model1](../../includes/RP/page1.md)]
|
||||
|
||||
This tutorial examines the Razor Pages created by scaffolding in the previous tutorial topic [Adding a model](xref:tutorials/razor-pages/model#scaffold-the-movie-model).
|
||||
|
||||
[View or download](https://github.com/aspnet/Docs/tree/master/aspnetcore/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie) sample.
|
||||
|
||||
## The Create, Delete, Details, and Edit pages.
|
||||
|
||||
Examine the *Pages/Movies/Index.cshtml.cs* Page Model:
|
||||
[!code-csharp[Main](razor-pages-start/snapshot_sample/RazorPagesMovie/Pages/Movies/Index.cshtml.cs)]
|
||||
|
||||
Razor Pages are derived from `PageModel`. By convention, the `PageModel`-derived class is called `<PageName>Model`. The constructor uses [dependency injection](xref:fundamentals/dependency-injection) to add the `MovieContext` to the page. All the scaffolded pages follow this pattern. See [Asynchronous code](xref:data/ef-rp/intro#asynchronous-code) for more information on asynchronous programing with Entity Framework.
|
||||
|
||||
When a request is made for the page, the `OnGetAsync` method returns a list of movies to the Razor Page. `OnGetAsync` or `OnGet` is called on a Razor Page to initialize the state for the page. In this case, `OnGetAsync` gets a list of movies and displays them.
|
||||
|
||||
When `OnGet` returns `void` or `OnGetAsync` returns`Task`, no return method is used. When the return type is `IActionResult` or `Task<IActionResult>`, a return statement must be provided. For example, the *Pages/Movies/Create.cshtml.cs* `OnPostAsync` method:
|
||||
|
||||
<!-- TODO - replace with snippet
|
||||
[!code-csharp[Main](razor-pages-start/snapshot_sample/RazorPagesMovie/Pages/Movies/Create.cshtml.cs?name=snippetALL)]
|
||||
-->
|
||||
|
||||
```csharp
|
||||
public async Task<IActionResult> OnPostAsync()
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return Page();
|
||||
}
|
||||
|
||||
_context.Movie.Add(Movie);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return RedirectToPage("./Index");
|
||||
}
|
||||
```
|
||||
Examine the *Pages/Movies/Index.cshtml* Razor Page:
|
||||
|
||||
[!code-cshtml[Main](razor-pages-start/snapshot_sample/RazorPagesMovie/Pages/Movies/Index.cshtml)]
|
||||
|
||||
Razor can transition from HTML into C# or into Razor-specific markup. When an `@` symbol is followed by a [Razor reserved keyword](xref:mvc/views/razor#razor-reserved-keywords), it transitions into Razor-specific markup, otherwise it transitions into C#.
|
||||
|
||||
The `@page` Razor directive makes the file into an MVC action — which means that it can handle requests. `@page` must be the first Razor directive on a page. `@page` is an example of transitioning into Razor-specific markup. See [Razor syntax](xref:mvc/views/razor#razor-syntax) for more information.
|
||||
|
||||
Examine the lambda expression used in the following HTML Helper:
|
||||
|
||||
```cshtml
|
||||
@Html.DisplayNameFor(model => model.Movie[0].Title))
|
||||
```
|
||||
|
||||
The `DisplayNameFor` HTML Helper inspects the `Title` property referenced in the lambda expression to determine the display name. The lambda expression is inspected rather than evaluated. That means there's no access violation when `model`, `model.Movie`, or `model.Movie[0]` are `null` or empty. When the lambda expression is evaluated (for example, with `@Html.DisplayFor(modelItem => item.Title)`), the model's property values are evaluated.
|
||||
|
||||
<a name="md"></a>
|
||||
### The @model directive
|
||||
|
||||
[!code-cshtml[Main](razor-pages-start/snapshot_sample/RazorPagesMovie/Pages/Movies/Index.cshtml?range=1-2&highlight=2)]
|
||||
|
||||
The `@model` directive specifies the type of the model passed to the Razor Page. In the preceding example, the `@model` line makes the `PageModel`-derived class available to the Razor Page. The model is used in the `@Html.DisplayNameFor` and `@Html.DisplayName` [HTML Helpers](https://docs.microsoft.com/aspnet/mvc/overview/older-versions-1/views/creating-custom-html-helpers-cs#understanding-html-helpers) on the page.
|
||||
|
||||
<!-- why don't xref links work?
|
||||
[HTML Helpers 2](xref:aspnet/mvc/overview/older-versions-1/views/creating-custom-html-helpers-cs)
|
||||
-->
|
||||
|
||||
<a name="vd"></a>
|
||||
### ViewData and layout
|
||||
|
||||
Consider the following code:
|
||||
|
||||
[!code-cshtml[Main](razor-pages-start/snapshot_sample/RazorPagesMovie/Pages/Movies/Index.cshtml?range=1-6&highlight=4-)]
|
||||
|
||||
The preceding highlighted code is an example of Razor transitioning into C#. The `{` and `}` characters enclose a block of C# code.
|
||||
|
||||
The `PageModel` base class has a `ViewData` dictionary property that can be used to add data that you want to pass to a View. You add objects into the `ViewData` dictionary using a key/value pattern. In the preceding sample, the "Title" property is added to the `ViewData` dictionary. The "Title" property is used in the *Pages/_Layout.cshtml* file. The following markup shows the first few lines of the *Pages/_Layout.cshtml* file.
|
||||
|
||||
[!code-cshtml[Main](razor-pages-start/snapshot_sample/RazorPagesMovie/Pages/NU/_Layout1.cshtml?highlight=6-)]
|
||||
|
||||
The line `@*Markup removed for brevity.*@` is a Razor comment. Unlike HTML comments (`<!-- -->`), Razor comments are not sent to the client.
|
||||
|
||||
Run the app and test the links in the project (**Home**, **About**, **Contact**, **Create**, **Edit**, and **Delete**). Each page sets the title, which you can see in the browser tab. When you bookmark a page, the title is used for the bookmark. *Pages/Index.cshtml* and *Pages/Movies/Index.cshtml* currently have the same title, but you can modify them to have different values.
|
||||
|
||||
The `Layout` property is set in the *Pages/_ViewStart.cshtml* file:
|
||||
|
||||
[!code-cshtml[Main](razor-pages-start/sample/RazorPagesMovie/Pages/_ViewStart.cshtml)]
|
||||
|
||||
The preceding markup sets the layout file to *Pages/_Layout.cshtml* for all Razor files under the *Pages* folder. See [Layout](xref:mvc/razor-pages/index#layout) for more information.
|
||||
|
||||
### Update the layout
|
||||
|
||||
Change the `<title>` element in the *Pages/_Layout.cshtml* file to use a shorter string.
|
||||
|
||||
[!code-cshtml[Main](razor-pages-start/sample/RazorPagesMovie/Pages/_Layout.cshtml?range=1-6&highlight=6)]
|
||||
|
||||
Find the following anchor element in the *Pages/_Layout.cshtml* file.
|
||||
|
||||
```cshtml
|
||||
<a asp-page="/Index" class="navbar-brand">RazorPagesMovie</a>
|
||||
```
|
||||
Replace the preceding element with the following markup.
|
||||
|
||||
```cshtml
|
||||
<a asp-page="/Movies/Index" class="navbar-brand">RpMovie</a>
|
||||
```
|
||||
|
||||
The preceding anchor element is a [Tag Helper](xref:mvc/views/tag-helpers/intro). In this case, it's the [Anchor Tag Helper](xref:mvc/views/tag-helpers/builtin-th/anchor-tag-helper). The `asp-page="/Movies/Index"` Tag Helper attribute and value creates a link to the `/Movies/Index` Razor Page.
|
||||
|
||||
Save your changes, and test the app by clicking on the **RpMovie** link. See the [_Layout.cshtml](https://github.com/aspnet/Docs/blob/master/aspnetcore/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie/Pages/_Layout.cshtml) file in GitHub.
|
||||
|
||||
### The Create code-behind page
|
||||
|
||||
Examine the *Pages/Movies/Create.cshtml.cs* code-behind file:
|
||||
|
||||
[!code-csharp[Main](razor-pages-start/snapshot_sample/RazorPagesMovie/Pages/Movies/Create.cshtml.cs?name=snippetALL)]
|
||||
|
||||
The `OnGet` method initializes any state needed for the page. The Create page doesn't have any state to initialize. The `Page` method creates a `PageResult` object that renders the *Create.cshtml* page.
|
||||
|
||||
The `Movie` property uses the `[BindProperty]` attribute to opt-in to [model binding](xref:mvc/models/model-binding). When the Create form posts the form values, the ASP.NET Core runtime binds the posted values to the `Movie` model.
|
||||
|
||||
The `OnPostAsync` method is run when the page posts form data:
|
||||
|
||||
[!code-csharp[Main](razor-pages-start/snapshot_sample/RazorPagesMovie/Pages/Movies/Create.cshtml.cs?name=snippetPost)]
|
||||
|
||||
If there are any model errors, the form is redisplayed, along with any form data posted. Most model errors can be caught on the client-side before the form is posted. An example of a model error is posting a value for the date field that cannot be converted to a date. We'll talk more about client-side validation and model validation later in the tutorial.
|
||||
|
||||
If there are no model errors, the data is saved, and the browser is redirected to the Index page.
|
||||
|
||||
### The Create Razor Page
|
||||
|
||||
Examine the *Pages/Movies/Create.cshtml* Razor Page file:
|
||||
|
||||
[!code-cshtml[Main](razor-pages-start/snapshot_sample/RazorPagesMovie/Pages/Movies/Create.cshtml)]
|
||||
|
||||
Visual Studio displays the `<form method="post">` tag in a distinctive font used for Tag Helpers. The `<form method="post">` element is a [Form Tag Helper](xref:mvc/views/working-with-forms#the-form-tag-helper). The Form Tag Helper automatically includes an [antiforgery token](xref:security/anti-request-forgery).
|
||||
Visual Studio displays the `<form method="post">` tag in a distinctive font used for Tag Helpers:
|
||||
|
||||
![VS17 view of Create.cshtml page](page/_static/th.png)
|
||||
|
||||
The scaffolding engine creates Razor markup for each field in the model (except the ID) similar to the following:
|
||||
|
||||
[!code-cshtml[Main](razor-pages-start/snapshot_sample/RazorPagesMovie/Pages/Movies/Create.cshtml?range=15-20)]
|
||||
|
||||
The [Validation Tag Helpers](xref:mvc/views/working-with-forms#the-validation-tag-helpers) (`<div asp-validation-summary` and ` <span asp-validation-for`) display validation errors. Validation is covered in more detail later in this series.
|
||||
|
||||
The [Label Tag Helper](xref:mvc/views/working-with-forms#the-label-tag-helper) (`<label asp-for="Movie.Title" class="control-label"></label>`) generates the label caption and `for` attribute for the `Title` property.
|
||||
|
||||
The [Input Tag Helper](xref:mvc/views/working-with-forms) (`<input asp-for="Movie.Title" class="form-control" />`) uses the [DataAnnotations](https://docs.microsoft.com/aspnet/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-6) attributes and produces HTML attributes needed for jQuery Validation on the client-side.
|
||||
[!INCLUDE[model1](../../includes/RP/page2.md)]
|
||||
|
||||
The next tutorial explains SQL Server LocalDB and seeding the database.
|
||||
|
||||
|
|
|
@ -1,26 +1,13 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
|
||||
<DotNetCliToolReference Include="BundlerMinifier.Core" Version="2.5.357" />
|
||||
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
|
||||
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.2" />
|
||||
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1" />
|
||||
|
||||
</ItemGroup>
|
||||
|
||||
<Target Name="PreBuildScript" BeforeTargets="PrepareForBuild">
|
||||
<Exec Command="bower install" />
|
||||
</Target>
|
||||
|
||||
<Target Name="PrePublishScript" BeforeTargets="PrepareForPublish">
|
||||
<Exec Command="dotnet bundle" />
|
||||
</Target>
|
||||
|
||||
</Project>
|
||||
|
|
Loading…
Reference in New Issue