这个样子
- @page指令
razor指令将文件转换为一个mvc操作,这意味着它可以处理请求。必须是第一个指令。 - @model
@model指令
指定传递到Razor页面的模型类型。
Index.cshtml页面标签
@*index page *@
@page
@model RazorPagesMovie.Pages.Movies.IndexModel
@{
ViewData["Title"] = "Index Page";
}
<h1>Index Page</h1>
<p>
<a asp-page="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Movie[0].ID)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].RleaseDate)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Genre)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Price)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Movie)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(x => item.RleaseDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(x => item.Price)
</td>
<td>
<a asp-page="./Edit" asp-route-id="@item.ID">Edit</a> |
<a asp-page="./Details" asp-route-id="@item.ID">Details</a>|
<a asp-page="./Delete" asp-route-id="@item.ID">Delete</a>
</td>
</tr>
}
</tbody>
</table>
后端
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.Data;
using RazorPagesMovie.Models;
namespace RazorPagesMovie.Pages.Movies
{
public class IndexModel : PageModel
{
private readonly RazorPagesMovie.Data.RazorPagesMovieContext _context;
public IndexModel(RazorPagesMovie.Data.RazorPagesMovieContext context)
{
_context = context;
}
public IList<Movie> Movie { get; set; }
public async Task OnGetAsync()
{
Movie = await _context.Movie.ToListAsync();
}
}
}
delete页面
@page
@model RazorPagesMovie.Pages.Movies.DeleteModel
@{
ViewData["Title"] = "Delete";
}
<h1>Delete</h1>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>Movie</h4>
<hr />
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Movie.Title)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Movie.Title)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Movie.RleaseDate)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Movie.RleaseDate)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Movie.Genre)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Movie.Genre)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Movie.Price)
</dt>
<dd class="col-sm-10">
@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-danger" /> |
<a asp-page="./Index">Back to List</a>
</form>
</div>
后端
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.Data;
using RazorPagesMovie.Models;
namespace RazorPagesMovie.Pages.Movies
{
public class DeleteModel : PageModel
{
private readonly RazorPagesMovie.Data.RazorPagesMovieContext _context;
public DeleteModel(RazorPagesMovie.Data.RazorPagesMovieContext 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.FirstOrDefaultAsync(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");
}
}
}