ユーザー登録ポストを受け付けるコントローラアクションを持つ.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>
あなたが登録の残りの部分を表示することができますか()? (復帰する前に少なくともモデルを埋めるビット) –
登録機能はいくつかの追加のクラスにまたがっています。簡単な説明では、フォームで渡された値を使用して新しいアカウントとユーザーオブジェクトを作成します。次に、LINQを使用してモデルを更新します。 – Jeremy