ASP.NET Core Razor 页面如何使用 IsPost?

作者:vkvi 来源:ITPOW(原创) 日期:2020-8-13

在很多 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>
}
相关文章