以下のような、2つ以上フォームが存在するViewを作っています。
C#
1 2Index.cshtml 3 4@using (Html.BeginForm("ActionA","SampleController",FormMethod.Post)) 5{ 6 @Html.ValidationSummary() 7 <label>入力欄1</label> 8 @Html.TextBox(model => model.input1) 9 <button type="submit"/>送信</button> 10} 11・・・ 12@using (Html.BeginForm("ActionB", "SampleController",FormMethod.Post)) 13{ 14 @Html.ValidationSummary() 15 <label>入力欄あ</label> 16 @Html.TextBox(model => model.inputa) 17 <button type="submit">送信</button> 18} 19
各POSTのアクションメソッドでは、処理後、SampleController/Indexにリダイレクトします。
処理が失敗した際は、TempDate[各アクション名-Message] に失敗した旨の文言(エラーメッセージ)を格納します。
エラーメッセージを画面に表示させるために、SampleController/Indexでは以下の処理をしてから、ビューを返します。
C#
1SampleController/Index 2 3if(TempData["アクション名-Message"] != null) { 4 ModelState.AddModelError("", (string)TempData["アクション名-Message"]); 5} 6
そして、Viewの@Html.ValidationSummary()でエラーメッセージを表示する・・・
というところまでは良かったのですが、
1つのエラーメッセージが、すべてのフォームの@Html.ValidationSummary()に表示されてしまいます。
リンク先を参考に、ボタンにname属性を足し、
Request.Form.AllKeys.Contains("ボタンのname")で判断しようとしましたが、
Request.Form.AllKeysに値が入っておらず、判断することができませんでした。
どうすれば、1つの@Html.ValidationSummaryだけに表示できるようになるでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/14 09:06