Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify validation summary on multiple forms

I have 2 forms on a page as follows:

@using (Html.BeginForm()) {     @Html.ValidationSummary()     @Html.Label("code", "Confirmation Code")     @Html.TextBox("code")     <input type="submit" value="Go" /> } @using (Html.BeginForm("SendConfirmation", "Auth")) {     @Html.ValidationSummary()     @Html.Label("email", "Email")     @Html.TextBox("email")     <input type="submit" value="Resend" /> } 

If SendConfirmation throws an error, there are 2 validation summary being displayed. How do I get the validation summary to target its own?

like image 332
Shawn Mclean Avatar asked Mar 17 '11 16:03

Shawn Mclean


2 Answers

Give the submit button a unique name on both your forms, like so:

@using (Html.BeginForm()) {     @Html.ValidationSummary()     @Html.Label("code", "Confirmation Code")     @Html.TextBox("code")     <input type="submit" name="login-top" value="Go" /> } @using (Html.BeginForm("SendConfirmation", "Auth")) {     @Html.ValidationSummary()     @Html.Label("email", "Email")     @Html.TextBox("email")     <input type="submit" name="login-main" value="Resend" /> } 

Then you can check whether a particular form has been submitted by checking the value of the request for the key that corresponds to the submit button and then conditionally display the validation summary ie. in the top form you would add:

if (Request.Form.AllKeys.Contains("login-top")) {     @Html.ValidationSummary() } 
like image 138
Dan Diplo Avatar answered Sep 26 '22 23:09

Dan Diplo


the solution is to draw the validation summary only when you validate your form

for more details check this blog post

like image 40
Mandoleen Avatar answered Sep 26 '22 23:09

Mandoleen