在很多 ASP.NET Core Razor 教程中,提到了 IsPost 这个属性,比如:
@if (IsPost) { <p>Posted</p> }
但是却说认不到这个 IsPost,我不认为 IsPost 是自带的,因为我觉得这个名字看起都有点奇怪,按微软的个性,应该是 IsPosted。
法一、不使用 IsPost,使用 Request.ContentLength
@if (Request.ContentLength > 0) { <p>Posted</p> }
法二、自定义 IsPost 属性
在 .cshtml.cs 中,自定义 IsPosted,再在 OnPost() 中为其设置为 true。
public class IndexModel : PageModel { public bool IsPosted { get; set; } public void OnPost() { IsPosted = true; } }
然后这样使用:
@if (Model.IsPosted) { <p>Posted</p> }