1. 一般的な状況
MVC フレームワークを使用したことのある人にとっては、MVC データ検証に精通していると思います。たとえば、次のようなモデルがあります。
効果:
はい、MVC は、いくつかの属性に特定の特性を追加することでデータを検証できます。これは誰にとっても馴染みのないことかもしれません。
それだけなら、何も問題ありません。
2. よくある状況
実際の開発では、ほとんどの場合、EF またはその他のメソッドを使用して、データベース内のすべてのテーブルまたはビューに、コード内で対応するクラス モデルを持たせる必要があります。データベースに一歩戻って、このクラスの一部の属性にデータ検証機能を追加したとしても、データベースの変更後にこれらのモデルを再生成すると、機能の前に追加した検証が失われます。この問題? 仮定します:
1 | 1 public class UserInfo2 {3 [Required(ErrorMessage = "UserName不可为空1111" )]4 public string UserName { get; set; }5 public string Sex { get; set; }6 public string Mobile { get; set; }7 public string Address { get; set; }8 }
|
ログイン後にコピー
UserInfo はデータベースによって生成されたモデルです。データベースによって生成されたモデルを変更すべきではありません。ただし、このモデルでは特定の属性に対してデータ検証を実行する必要があります。たとえば、UserName 属性に対して非 null 検証を実行する必要があります。
通常、誰もが部分分類を考えます。はい、部分分類によって上記の問題を解決できます。
まず、モデル内のクラスにキーワード Partial を追加し、このモデルの部分クラスを作成します。
1 2 | 1 @using (Html.BeginForm())
2 { 3 @Html.AntiForgeryToken() 4 <div class = "form-horizontal" > 5 <h4>UserInfo</h4> 6 <hr /> 7 @Html.ValidationSummary(true, "" , new { @ class = "text-danger" }) 8 <div class = "form-group" > 9 @Html.LabelFor(model => model.UserName, htmlAttributes: new { @ class = "control-label col-md-2" })10 <div class = "col-md-10" >11 @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @ class = "form-control" } })12 @Html.ValidationMessageFor(model => model.UserName, "" , new { @ class = "text-danger" })13 </div>14 </div>15 <div class = "form-group" >16 @Html.LabelFor(model => model.Sex, htmlAttributes: new { @ class = "control-label col-md-2" })17 <div class = "col-md-10" >18 @Html.EditorFor(model => model.Sex, new { htmlAttributes = new { @ class = "form-control" } })19 @Html.ValidationMessageFor(model => model.Sex, "" , new { @ class = "text-danger" })20 </div>21 </div>22 <div class = "form-group" >23 @Html.LabelFor(model => model.Mobile, htmlAttributes: new { @ class = "control-label col-md-2" })24 <div class = "col-md-10" >25 @Html.EditorFor(model => model.Mobile, new { htmlAttributes = new { @ class = "form-control" } })26 @Html.ValidationMessageFor(model => model.Mobile, "" , new { @ class = "text-danger" })27 </div>28 </div>29 <div class = "form-group" >30 @Html.LabelFor(model => model.Address, htmlAttributes: new { @ class = "control-label col-md-2" })31 <div class = "col-md-10" >32 @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @ class = "form-control" } })33 @Html.ValidationMessageFor(model => model.Address, "" , new { @ class = "text-danger" })34 </div>35 </div>36 <div class = "form-group" >37 <div class = "col-md-offset-2 col-md-10" >38 <input type= "submit" value= "Create" class = "btn btn-default" />39 </div>40 </div>41 </div>42 }
|
ログイン後にコピー
ただし、これにより、クラス内に重複した属性が存在するというエラーが表示されます。はい、一部のクラスでは、属性に同じ名前を付けることができません。では、何をすべきでしょうか? MVC フレームワークはすでに解決策を提供しています。
次のように書くことができます:
1 2 | 1 public class UserInfo2 {
3 public string UserName { get; set; }4 public string Sex { get; set; }5 public string Mobile { get; set; }6 public string Address { get; set; }7 }
|
ログイン後にコピー
このようにして、上記の問題は簡単に解決されます。
以上がMVC でのデータ検証例の概要の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。