2012-01-19 13 views
1

私は現在、研究を行っているにもかかわらず、ASP.NET MVC3でjqueryの控えめなクライアント側の検証が失敗した場合のサーバー側の検証を実施

エンティティフレームワーク4でPOCOを使用して、データをデータベースに保存し直しています。データ注釈を使用して、重複している検証の量を削減しようとしています(従来の従来のASPソリューションでは、アプリケーションの3つの異なるレベルでSAME検証が行われていました)。

私たちのモデルには、他のテーブルに対して検証されたフィールドが有効であることが含まれています(モデルではドロップダウンを使用しないため、ユーザーは何でも入力できます)。たとえば、部屋の情報を建物に格納しています。部屋には「ルームタイプ」というフィールドがあります。有効なルームタイプは別のテーブルで定義されています。

また、クライアント側での即時検証も必要です。たとえば、数字フィールドは0から32767の間でなければなりません。ユーザーが-1を入力すると、-1が無効であることをユーザーに即座に応答するためにクライアント側の検証が使用されます。クライアント側の検証を有効にし、データアノテーションを使用することで、これを行います。

のWeb設定:

<appSettings> 
    <add key="webpages:Version" value="1.0.0.0" /> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
</appSettings> 

モデル:

public class Room : IValidatableObject 
{ 
    // I'm only showing the relevant properties... 

    // this is a field that's validated based on another table in the database. 
    // (In the model we are using autocomplete instead of a dropdown -- it's a long 
    // story --, so potentially invalid data can be passed through the form... 
    [DisplayName("Room Type"), MaxLength(5)]   
    public String RoomType { get; set; } 

    // A number field, with a range. 
    [Range(0, 32767), DisplayName("Maximum Seats")] 
    public Int16? MaxStudents { get; set; } 

    // do server side validation for this entity. 
    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) 
    { 
     var validateErrors = new List<ValidationResult>(); 

     // make sure room type is a valid type 
     if(!String.IsNullOrEmpty(RoomType)) { 
      // hit database, which is a little weird since we need to create a context, 
      // and I know that might make some people's brains explode 
     } 

     // return all of the errors 
     return validateErrors; 
    } 
} 

コントローラー:

// I didn't include all the actions, just edit 

    public ActionResult Edit(Int32 Building, String RoomID) 
    { 
     // return a single room and show the detail information in edit mode. 
     Room room = findRoom(Building, RoomID); 
     return View(room); 
    } 

    [HttpPost] 
    public ActionResult Edit(Int32 Building, String RoomID, FormCollection collection) 
    { 
     // get the current room from the database. 
     Room room = findRoom(Building, RoomID); 

     // save the room being edited, but don't touch the key fields. 
     if (TryUpdateModel(room, null, null, new string[] {"Building", "RoomID"})) 
     { 
      // if the entity changed, audit and save 
      if (db.Entry(room).State == EntityState.Modified) 
      { 
       db.setLastUpdate(room); // this is a function in our context for auditing 
       db.SaveChanges(); 
      }     
     } 
     return View(room); 
    } 

ビュー: (オートコンプリートを作成するために使用されるJavaScriptを表示するために悩みません... )

@using (Html.BeginForm()) { 
    @Html.ValidationSummary(false, "The following errors occured when attempting to save this Room:") 
    <fieldset> 
     <legend>Room</legend> 
     <div class="field-block"> 
      <div class="editor-label"> 
       @Html.LabelFor(model => model.Building) 
      </div> 
      <div class="editor-field"> 
       @Html.DisplayFor(model => model.Building) 
      </div> 
     </div> 
     <div class="field-block"> 
      <div class="editor-label"> 
       @Html.LabelFor(model => model.RoomID) 
      </div> 
      <div class="editor-field"> 
       @Html.DisplayFor(model => model.RoomID) 
      </div> 
     </div> 
     <div class="field-block"> 
      <div class="editor-label"> 
       @Html.LabelFor(model => model.RoomType) 
      </div> 
      <div class="editor-field"> 
       @Html.EditorFor(model => model.RoomType) 
      </div> 
     </div> 

     <div class="field-block"> 
      <div class="editor-label"> 
       @Html.LabelFor(model => model.MaxStudents) 
      </div> 
      <div class="editor-field"> 
       @Html.EditorFor(model => model.MaxStudents) 
       @Html.ValidationMessageFor(model => model.MaxStudents) 
      </div> 
     </div> 
     <p> 
      <input type="submit" value="Save" /> 
     </p> 
    </fieldset> 
} 

私は気づいていますが、ユーザーのタイプは-1最大席数]フィールドで、クライアント側の検証は、ユーザーを発射し、できるようになる場合は、ユーザーがクリックした場合値は0から32767

の間でなければならないことを知っているということですサブミットすると、クライアントの検証が再度実行され、フォームの上部にある検証サマリーに表示されます。

ユーザーが[最大席数]に有効な値を入力したが、[ルームタイプ]フィールドに誤った値を入力した場合、クライアント側の妥当性検査でエラーは発生していないことが示されます(ルームタイプはクライアントで検証されていないため、ユーザーが[Submit]をクリックすると、TryUpdateModel()呼び出し中にIValidateObject.Validate関数が呼び出され、検証エラーが返されます。このエラーは、フォームの上部にある検証サマリーのページに表示されます。

しかし、ユーザーが間違った番号(-1)と無効なルームタイプを入力して[送信]をクリックすると、クライアント側の検証は実行されますが、サーバー側の検証は実行されません。関連するエラー。

JavaScriptがクライアント側とサーバー側の両方の検証の両方を呼び出すために使用できる設定や些細なことはありますか?

ない場合は、私が(値をチェックしている私の唯一の選択肢はに彼らがフィールドからフィールドへ行くようにユーザーにあまりフィードバックを与える、またはルームタイプの検証を行いますすべての検証サーバ側を、行うことだと思いますAjaxコールを使用してルームタイプのテーブル)を作成すると、作業が重複します。

アイデア?

答えて

2

私はあなたが探しているのはremote validationだと思います。これにより、検証のためにajaxを使用することについて述べたことを行うことができます。あなたはクライアント上で番号の検証を行い、サーバー上で部屋の検証を行います。その結果はクライアント側の検証で報告されます。

+0

私はそれを調べます。それは有望に見える:) –

+0

私はこれが働いている。ニース:) –

関連する問題