1.从前端的
View
通过默认,ID是参数。变成 https://localhost:7030/Employees/Details?ID=1
2.https://localhost:7030/Employees/Details?ID=1
到项目下 Employees文件夹下的Details.cshtml.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using RazorPagesTutorialModels;
using RazorPagesTutorialServices;
namespace RazorPagesTutorial.Pages.Employees
{
public class DetailsModel : PageModel
{
private readonly IEmployeeRepository employeeRepository;
public DetailsModel(IEmployeeRepository employeeRepository)
{
this.employeeRepository=employeeRepository;
}
public Employee Employee { get; private set; }
public void OnGet(int id)
{
Employee=employeeRepository.GetEmployee(id);
}
}
}
模型绑定将路由参数值绑定到OnGet()方法参数上。
@page 可以传值,也可以限制路径(匹配原则),还可以限制传参的类型
限制参数的类型如下:
还可以限制参数的范围,如:
@page "/employees/view/{id:even}"
@model RazorPagesTutorial.Pages.Employees.DetailsModel
@{
ViewData["Title"] = "Detail";
var photPath = "~/images/" + (Model.Employee.PhotoPath ?? "noimage.jpg");
}
Detail
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
namespace RazorPagesTutorial
{
public class EventConstraint:IRouteConstraint
{
public bool Match(HttpContext? httpContext, IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
{
//throw new NotImplementedException();
int id;
if (Int32.TryParse(values["id"].ToString(), out id))
{
if(id %2== 0)
{
return true;
}
}
return false;
}
}
}
在startup的配置文件里面
添加
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddSingleton();
services.Configure(options =>
{
options.ConstraintMap.Add("even",typeof(EventConstraint));
});
}
完成修改的整个流程
1.页面点击修改
所以页面有修改的按键,并且传值id
我们可以在列表的每条信息中设计修改的按钮,点击按钮asp-page,访问 https://localhost:7030/employees/edit/?id=1
2.根据链接 https://localhost:7030/employees/edit/?id=1 找到
Edit的cs文件
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using RazorPagesTutorialModels;
using RazorPagesTutorialServices;
namespace RazorPagesTutorial.Pages.Employees
{
public class EditModel : PageModel
{
private readonly IEmployeeRepository employeeRepository;
//先访问构造函数
public EditModel(IEmployeeRepository employeeRepository) {
this.employeeRepository = employeeRepository;
}
public Employee Employee { get; set; }
//访问OnGet方法,此方法判断是否能查询到信息,重定向到不同的页面
public IActionResult OnGet(int id)
{
Employee=employeeRepository.GetEmployee(id);
if(Employee == null)
{
return NotFound();
}
//进入Edit的页面
return Page();
}
//进入Edit的页面后
public IActionResult OnPost(Employee employee)
{
Employee=employeeRepository.Update(employee);
return RedirectToPage("Index");
}
}
}
注意Onpost方法是form表单提交,提交的对象,然后走update的方法处理,Services层,然后返回Index页面。
@page
@model RazorPagesTutorial.Pages.Employees.EditModel
@{
ViewData["Title"] = "Edit";
}
Edit
需要注意的是:前端所引用的对象,需要在_ViewImports.cshtml里面写好引用。例如
//using 这个地方是namespace
@using RazorPagesTutorial
@using RazorPagesTutorialModels
@namespace RazorPagesTutorial.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
关于页面验证