[BindProperty(SupportsGet = true)]

  • 723
  • 0

[BindProperty]、[BindProperty(SupportsGet = true)]的差異處在哪?

教學範例有提到

[BindProperty] 使用與屬性相同的名稱來繫結表單值和查詢字串。 
[BindProperty(SupportsGet = true)] 在 HTTP GET 要求上進行系結是必要的。

阿鬼,可以說中文嗎?

簡單來說,加上 [BindProperty] Attribute 與 .cshtml 中的 <input asp-for="xxx"> 呼應,OnPost() 時 Razor Page 也會自動將前端傳入的欄位值寫入,
可省去自己 Request.Form["xxx"] 取值的麻煩

而[BindProperty(SupportsGet = true)],則是若需於OnGet()取得?xxx=某某的值時使用

PS. [BindProperties] 使用則更廣,套用至控制器或 PageModel 類別

[BindProperties(SupportsGet = true)]
public class CreateModel : InstructorsPageModel
{
    public Instructor Instructor { get; set; }

參考: