【发布时间】:2020-08-26 13:43:04
【问题描述】:
我无法让 Razor Pages 工作。它将显示索引页面,仅此而已。页面上没有链接,也不会显示任何数据。编译器会完全忽略所有辅助方法,甚至不会用不同的颜色突出显示它们。
当页面加载时,它将调用 Get 函数并显示页面,但视图上的任何功能都不起作用。我已经删除了所有内容,所以我只有一页,上面有几个发布按钮,但它仍然不会调用 Post 方法。有人知道发生了什么吗?
这是我的观点:
@page
@model FloorCore.Areas.Jobs.Pages.JobIndexModel
@{
}
<form method="post">
<button class="btn btn-default">Click to post</button>
<button type="submit" class="btn btn-primary">Click to post</button>
</form>
这里是控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace FloorCore.Areas.Jobs.Pages
{
public class JobIndexModel : PageModel
{
public void OnGet()
{
int for_breakpoint = 0;
}
public void OnPost()
{
int for_breakpoint = 0;
}
}
}
单击任一按钮都会返回: 此页面无法正常工作。 如果问题仍然存在,请联系网站所有者。 HTTP 错误 400
控制器上的 OnPost 方法没有被调用。
【问题讨论】:
-
A 400 状态是错误请求:在 ASP.NET 调用您的模型的方法之前可能存在问题。
-
@SimonBailey 您肯定缺少用于 OnPost() 操作的
[HttpPost]属性装饰器,因为您使用的是<form method="post">形式的 post 方法。您还需要表单标签中的asp-action='yourAction'属性。更不用说你必须像之前所说的那样检查你的模型。
标签: c# asp.net-core razor-pages