.Net RazorPages 初体验(页面动作如何请求后台)

都说 RazorPages 比 MVC 方便, 但是第一次接触坑可能会麻爪, 特别是使用过MVC后再来第一次使用 RazorPages. 当然, 这两是可以混用的,RazorPages基于MVC.

如果页面有个操作要请求后台,怎么搞?

有点麻...

一下以 `Index` 页面 (文件 "Pages\Index.html" 和 "Pages\Index.html.cs"), 和 删除动作`Del`为例说明

/// 请求时,需要路径`handler`参数来指定接收的方法(如:?handler=Del)
/// 
/// 表单不能添加`action`标签,否则不生成验证属性字段`__RequestVerificationToken` !
/// 没有这个验证字段,服务端无法验证提交的合法性,会直接返回`400`;
/// 
/// 只能使用如下方式才能使后端(OnPostXXXXX, 如:OnPostDel)正确接收请求(以下其一即可,第一个实际是生成`formaction`标签):
/// 1. 使用`asp-page`(如:asp-page="/Index")和`asp-page-handler`(如:asp-page-handler="Del")标签在表单提交按钮中, 验证提交的内容; 否则提交到默认地址(OnPost)
/// 2. 添加`formaction`标签指定路径(如:formaction="?handler=Del")到表单提交按钮中;否则提交到默认地址(OnPost)

代码示例:

 Pages\Index.html.cs

[BindProperty]
public Models.PicInfo? PicInfo { get; set; }

/// 默认 Post
public IActionResult OnPost() {
    if (!ModelState.IsValid) {
        return Content("{\"code\":4,\"msg\":\"参数错误\"}", "application/json");
    }

    return Content("{\"code\":0}", "application/json");
}
/// 删除动作 Post
public IActionResult OnPostDel() {
    if (!ModelState.IsValid) {
        return Content("{\"code\":4,\"msg\":\"参数错误\"}", "application/json");
    }

    return Content("{\"code\":0}", "application/json");
}

 

Pages\Index.html

 1 <div class="row">
 2     <div class="col-md-4">
 3         <form method="post">
 4             <input asp-for="PicInfo!.Act" value="Del" hidden class="form-control" />
 5             <input asp-for="PicInfo!.Album" value="Del" hidden class="form-control" />
 6             <div asp-validation-summary="ModelOnly" class="text-danger"></div>
 7             <div class="form-group">
 8                 <label asp-for="PicInfo!.Name" class="control-label"></label>
 9                 <input asp-for="PicInfo!.Name" class="form-control" />
10                 <span asp-validation-for="PicInfo!.Name" class="text-danger"></span>
11             </div>
12             <div class="form-group">
13                 <label asp-for="PicInfo!.Path" class="control-label"></label>
14                 <input asp-for="PicInfo!.Path" class="form-control" />
15                 <span asp-validation-for="PicInfo!.Path" class="text-danger"></span>
16             </div>
17             <div class="form-group">
18                 <label asp-for="PicInfo!.Date" class="control-label"></label>
19                 <input asp-for="PicInfo!.Date" class="form-control" value="2022-10-16"/>
20                 <span asp-validation-for="PicInfo!.Date" class="text-danger"></span>
21             </div>
22             <div class="form-group">
23                 <input type="submit" value="Create" class="btn btn-default" formaction="?handler=Del" @*asp-page="/Index" asp-page-handler="Del"*@/>
24             </div>
25         </form>
26     </div>
27 </div>
28 
29 @section Scripts {
30 @*  自动验证填写  *@ 
31    @{
32         await Html.RenderPartialAsync("_ValidationScriptsPartial");
33     }
34 }

 

Models\PicInfo.cs

 1 public class PicInfo {
 2     [Display(Name = "相册")]
 3     public string? Album { get; set; }
 4 
 5     [Display(Name = "图片名称")]
 6     [StringLength(10, MinimumLength = 3)]
 7     public string? Name { get; set; }
 8 
 9     [Display(Name = "图片地址")]
10     public string? Path { get; set; }
11 
12     [Display(Name = "动作")]
13     public string? Act { get; set; }
14 
15     [Display(Name = "时间")]
16     [DataType(DataType.Date)]
17     public DateTime Date { get; set; }
18 }

 

 

(PS:you cankao)

posted @ 2022-10-15 21:42  _Ong  阅读(322)  评论(0编辑  收藏  举报