2010-11-29 14 views
0

私はEntity Frameworkを使用しているASP.NET MVC 2.0アプリケーションを持っています。私のすべてのビューはビューモデルを使用し、そのほとんどは複雑です。意味...編集されるオブジェクトは、ビューモデル自体のプロパティではなく、ビューモデルのプロパティです。ModelState.IsValidは "新しい"フォームでは動作しますが、 "編集"フォームでは機能しません

私はデータアノテーションを持つ部分クラスを使用しており、コントローラ内のPOSTアクション内でModelState.IsValidをチェックしています。

私は、3つのフィールドを持つ単純なオブジェクトの "NEW"フォームと "EDIT"フォームを持っています!

ModelState.IsValidチェックは新しいフォームで機能し、空白のフォームを送信しようとすると正しい「必須フィールド」エラーが表示されます。私は例外を取得

しかし、私はEDITフォームをロードし、必要とされるいくつかのテキストボックスから値をクリアし、フォームを送信した場合、私は、検証エラーを取得しない、:の子要求を実行

エラーハンドラ 'System.Web.Mvc.HttpHandlerUtil + ServerExecuteHttpHandlerWrapper'。

私の質問は、FormCollectionの代わりにロードされたビューモデルオブジェクトの値を見ているので、ModelState.IsValidはEDITフォームでは機能しませんか?



    // this one does not validate 

     [AcceptVerbs(HttpVerbs.Post)] 
     public ActionResult Edit(int accountStatusKey, AccountStatusEditViewModel model, FormCollection values) 
     { 
      if (ModelState.IsValid) 
      { 
       db.UpdateAccountStatus(accountStatusKey, values); 
       return RedirectToAction("States"); 
      } 
      else 
      { 
       return View("Edit", model); 
      } 
     } 


    // this one does validate 

     [AcceptVerbs(HttpVerbs.Post)] 
     public ActionResult New(AccountStatusNewViewModel model, FormCollection values) 
     { 
      if (ModelState.IsValid) 
      { 
       db.AddAccountStatus(values); 

       return View("States", new AccountStatusStatesViewModel()); 
      } 
      else 
      { 
       return View("New", model); 
      } 
     } 

    // how I arrive AT the edit form 

     [AcceptVerbs(HttpVerbs.Get)] 
     public ActionResult Edit(int accountStatusKey) 
     { 
      return View("Edit", new AccountStatusEditViewModel(accountStatusKey)); 
     } 

    // and finally, the view model code 

    public class AccountStatusEditViewModel : ViewModelBase 
    { 

     public AccountStatus AccountStatus { get; private set; } 

     public IEnumerable States { get; private set; } 

     public List StatusTypes { get; private set; } 

     public AccountStatusEditViewModel(int accountStatusKey) 
     { 
      AccountStatus = db.GetAccountStatusByKey(accountStatusKey); 
      States = db.GetAllStates(); 

      StatusTypes = new List(); 
      StatusTypes.Add("Primary Status"); 
      StatusTypes.Add("Secondary Status"); 
      StatusTypes.Add("External Status"); 
     } 

     public AccountStatusEditViewModel() 
     { 
     } 

    } 

    // this action method does not work at all either - no db updating, no validation 
    // the page simply redirects to "States" view, which should only happen if the db 
    // was being updated, right? But nothing is changing in the DB, and the validation 
    // never happens. 

     [AcceptVerbs(HttpVerbs.Post)] 
     public ActionResult Edit(AccountStatusEditViewModel model) 
     { 
      if (ModelState.IsValid) 
      { 
       if (TryUpdateModel(model, "AccountStatus")) 
       { 
        return RedirectToAction("States"); 
       } 
       else 
       { 
        return View("Edit", model); 
       } 
      } 
      else 
      { 
       return View("Edit", model); 
      } 

     } 


+0

両方のアクション方法を投稿できますか? – Mariusz

+0

コードが投稿されました – Blackcoil

+0

最後のコード例では、 'public ActionResult Edit(AccountStatusEditViewModel model)'何がうまくいかないのですか?このモデルは有効であり、状態ビューにリダイレクトされると正しく更新されます。モデルに無効​​な値が含まれていると予想されるときにもそれを行いますか? – Michel

答えて

0

MVCの2.0バージョン以降、私はもはやformcollectionを使用していません。

私はポストを持っているとき、私はこれだけのように、アクションのパラメータでのviewmodelを使用

:「それは私のモデル(日時フィールドに入力されたblablaとき、または作成することはできません場合は

[HttpPost] 
public ActionResult Activatecard(ActivateCardViewModel model) 
{ 

System.ComponentModel.DataAnnotations名前空間の検証属性を使用しています)ModelState.IsValidが偽と等しくなります。

私は空のasp.net mvc2アプリケーションを作成しました。これは標準テンプレートがログオンに使用したモデルでしたアクション。

+0

このページのviewmodelは複雑で、私が更新しようとしている実際のオブジェクトはviewmodelのプロパティではなく、viewmodelのプロパティです。私は今ビデオモデルのコードを添付します。 – Blackcoil

+0

viewmodelのみを追加した場合、またはモデルを検証していない場合のみ例外が発生しますか? – Michel

0

FromCollectionパラメーターを削除してください。ビューモデルにはデフォルトのModelBindersを使用できます。 Asp.net MVCは、フォームからの値をモデルにマップしようとします。

あなたは編集フォームにつながるアクション方法を投稿してください。

+0

コードブロックの最後にGETアクションメソッドが追加されました。 – Blackcoil

+0

Michelからのアプローチを使用してください(上記の記事を参照)。これはあなたのために働くはずです。 – Mariusz

+0

私はこのスタイルに変更しましたが、それはまだ何もしません。 DBは更新されず、検証はまったく行われません。私はコードabeoveを添付します。 – Blackcoil

関連する問題