2011-01-24 7 views
0

私はカスタムメンバーシッププロバイダーを実装しており、次のクラスを持っています。カスタムメンバーシッププロバイダーのModelState

public class ProfileCommon : ProfileBase 
{ 
    #region Members 
    [Required(ErrorMessage="Required")] 
    public virtual string Title 
    { 
     get { return ((string)(this.GetPropertyValue("Title"))); } 
     set { this.SetPropertyValue("Title", value); } 
    } 

私のコントローラでは、次のことを実行します。

[HttpPost] 
    [Authorize] 
    public ActionResult EditInvestorRegistration(FormCollection collection) 
    { 
     ProfileCommon profileCommon= new ProfileCommon(); 
     TryUpdateModel(profileCommon); 

タイトルがエラーに含まれていない場合、これは失敗します。

オブジェクト 'Models.ProfileCommon'のプロパティの[タイトル]が次の例外を送出しました: '設定プロパティ'タイトル 'が見つかりませんでした。

属性[Required...を削除すると問題なく動作しますが、オブジェクトに対して自動検証が行われなくなりました。

今、各プロパティを一度にチェックして問題を回避することができますが、DataAnnotationsを使用して私の仕事をしたいと思っています。

アイデア?

public class ProfileViewModel 
{ 
    [Required] 
    public string Title { get; set; } 
} 

してから、コントローラにビューモデルとモデルクラス間の変換にAutoMapperを使用することができます。

答えて

1

あなたの代わりにビューモデルの動作入力などのカスタムプロファイルのクラスを使用していることを奇妙に思えますプロフィールを更新します:

[HttpPost] 
[Authorize] 
public ActionResult EditInvestorRegistration(ProfileViewModel profileViewModel) 
{ 
    ProfileCommon profileCommon = AutoMapper.Map<ProfileViewModel, ProfileCommon>(profileViewModel); 
    ... 
} 
+0

私はあなたの権利を考えています。代わりにビューモデルに移動します。ありがとう – griegs

関連する問題