2009-02-26 4 views
4

ユーザー登録ポストを受け付けるコントローラアクションを持つ.net mvcアプリケーションがあります。 私は以下のUIフィールドを持っています:emailaddress、firstname、lastname、password、およびconfirmpassword。これらのフィールドの一部はモデルオブジェクトに属しません(つまり、確認パスワードはユーザーモデルには属しません)。パスワードのみです。登録フォームはログインフォームと同じビューにあります。だから私は同じビューで独立したフォームにしなければならず、それぞれ別々のアクションにポストする。Asp.NET MVCモデルバインディング - バインドパラメータ属性を使用して単純な型のプレフィックスを割り当てる

私は、フォーム要素に接頭辞を割り当てて、レジスタとログインの間の同様のフィールドを区切ることができると考えました。私が持っていた問題は、エラーが発生した場合、ビューが再ロードされ、検証メッセージが表示されましたが、電子メール(ログインと登録に存在する)のようなフィールドには、さらに、私はログインフィールドとレジスタフィールドの両方より上の検証の要約を持っていました。登録中にエラーが発生した場合、両方の検証要約にエラーメッセージが入力されました。フィールド(register.fieldnameとlogin.fieldname)に接頭辞を割り当てると、これらの問題を解決できるかもしれません。

したがって、登録アクションが投稿を処理するとき、登録フォームフィールドの値は検出されず、nullが返されます。以下は、このアクションに使用されるメソッドシグネチャです。

ここで何が起こっているかについての入力は素晴らしいものです。 ジェレミー

public ActionResult Register([Bind(Prefix = "Register")] string emailAddress, [Bind(Prefix = "Register")] string firstName, [Bind(Prefix = "Register")] string LastName, [Bind(Prefix = "Register")] string password, [Bind(Prefix = "Register")] string confirmPassword) 

と、次はそれが登録フォームを表し、私のUIからです....

<h2>Create a New Account</h2> 
    <p> 
     Use the form below to create a new account. 
    </p> 
    <p> 
     Passwords are required to be a minimum of <%=Html.Encode(ViewData["PasswordLength"])%> characters in length. 
    </p> 
    <%= Html.ValidationSummary() %> 

    <% using (Html.BeginForm("register", "account")) { %> 
    <div> 
     <fieldset> 
     <legend>Account Information</legend> 

     <table> 
      <tr> 
      <td><label for="EmailAddress">Email Address:</label></td> 
      <td> 
       <%= Html.TextBox("Register.EmailAddress") %> 
       <%= Html.ValidationMessage("Register.EmailAddress")%> 
      </td> 
      </tr> 
      <tr> 
      <td><label for="FirstName">First Name</label></td> 
      <td> 
       <%= Html.TextBox("Register.FirstName")%> 
       <%= Html.ValidationMessage("Register.FirstName")%> 
      </td> 
      </tr> 
      <tr> 
      <td><label for="LastName">Last Name</label></td> 
      <td> 
       <%= Html.TextBox("Register.LastName")%> 
       <%= Html.ValidationMessage("Register.LastName")%> 
      </td> 
      </tr>   
      <tr> 
      <td><label for="password">Password:</label></td> 
      <td> 
       <%= Html.Password("Register.password")%> 
       <%= Html.ValidationMessage("Register.password")%> 
      </td> 
      </tr> 
      <tr> 
      <td><label for="confirmPassword">Confirm password:</label></td> 
      <td> 
       <%= Html.Password("Register.confirmPassword")%> 
       <%= Html.ValidationMessage("Register.confirmPassword")%> 
      </td> 
      </tr> 
      <tr> 
      <td colspan="2" class="alignright"> 
       <input type="submit" value="Register" /> 
      </td> 
      </tr> 
     </table> 
     </fieldset> 
    </div> 
    <% } %> 
</div> 
+0

あなたが登録の残りの部分を表示することができますか()? (復帰する前に少なくともモデルを埋めるビット) –

+0

登録機能はいくつかの追加のクラスにまたがっています。簡単な説明では、フォームで渡された値を使用して新しいアカウントとユーザーオブジェクトを作成します。次に、LINQを使用してモデルを更新します。 – Jeremy

答えて

6

私は接頭辞で、[バインド]を使用するために考えて

感謝複雑なタイプを使用する必要があります。同じプレフィックスを各パラメータに関連付けていますが、これはMVCフレームワークの動作とは関係ありません。あなたは、フォームのポストを処理するための型を作成することができます

public class RegistrationForm { 
string EmailAddress {get;set;} 
string FirstName {get;set;} 
string LastName {get;set;} 
string Password {get;set;} 
string ConfirmPassword {get;set;} 
} 

次にあなたがあなたの署名を変更することができます。

public ActionResult Register(RegistrationForm register) { 
... 
} 
+0

そのようなものは吸います。 3つのプロパティが必要な場合は、なぜ複合型にバインドする必要がありますか?ひどい欠点のようだ。 –

0

それはint型と文字列のような単純なタイプ、バインドのためにそれを思わ接頭辞はパラメータのモデル名を上書きします。たとえば、アクションメソッドを使用して、DefaultModelBinderはすべてのパラメータに対して「登録」という名前を探します。

これを回避し、複雑な型を必要としないようにするには、MvcがHTMLマークアップにレンダリングするときに完全修飾名を指定する必要があります。

あなたのアクションメソッドにこれを適用すると、それは次のようになります。

public ActionResult Register([Bind(Prefix = "Register.EmailAddress")] string emailAddress, [Bind(Prefix = "Register.FirstName")] string firstName, [Bind(Prefix = "Register.LastName")] string LastName, [Bind(Prefix = "Register.Password")] string password, [Bind(Prefix = "Register.onfirmPassword")] string confirmPassword) 
関連する問題