0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

役割

• for 属性を自動生成してくれる
• モデルの Display 名や DataAnnotations を反映する
• バリデーションメッセージと連動する

for 属性とは

要素がどのフォームコントロール(inputやbuttonなど)と関連付けられているかを明示するために使われる属性です。

具体例

Models/Customer.cs
        [Required(ErrorMessage = "氏名(漢字)は必須です。")]
        [MaxLength(100)]
        public string FullName { get; set; } = string.Empty;

モデルで、エラーメッセージ、MaxLengthを定義。

Views/LifeInsuranceMvc/AddCustomer.cshtml
    <div>
        <label asp-for="FullName">氏名</label>
        <input asp-for="FullName" class="form-control" />
        <span asp-validation-for="FullName" class="text-danger"></span>
    </div>

asp-for="FullName"によって、モデルのエラーメッセージ、MaxLengthの設定が連動されます。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?