LoginSignup
0
0

RazorPages備忘録#1

Posted at

RazorPagesの備忘録#1

PageのフォームのポストでModelにデータを渡してアクセスしたいプロパティがある場合は、

BindProPertyを使用する。

以下の例はViewのHTMLに名前、年齢、性別を入力してバインドさせたい場合。

Person.cs
public class Person
{
    [Key]
    public int Id { get; set; }
    
    [Required]
    [DisplayName("名前")]
    public string Name { get; set; }

    [Required]
    [DisplayName("年齢")]
    public int Age { get; set; }

    [DisplayName("性別")]
    public string Gender { get; set; }
}

PersonModel.cshtml.cs
public class PersonModel : PageModel
{
    private readonly ApplicationDbContext _context;

    [BindProperty]
    public Person person { get; set; }
    //以下処理が続く

View側のcshtmlのフォームにてPOSTをした場合、入力した内容をPOSTメソッドで引数を受け取る処理がMVCでは必要の認識だが、
RazorPageでBindPropertyを用いることで引数の処理を書かなくてもよくなっている。

サーバーバリデーションを行うときはどうすればよいのか疑問はあるが、これから勉強していくうちに解決するかもしれない。
クライアント側のバリデーションで事足りる場合はそもそも必要ないのかもしれないけども。。。

こういうアーキテクチャをMVVMというのだっけ??

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0