30

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?

4 Answers 4

21

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()
}
Sign up to request clarification or add additional context in comments.

1 Comment

This works ok, but you will lose the client side validation summary on the next load since the placeholder elements won't be there. You could add an else statement and render the <div> and empty <ul> elements that are normally there.
6

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

for more details check this blog post

1 Comment

This IS the answer to the issue of two validation summaries. Only draw the validation summary that you want to show. For example, the user clicks on login. In the controller, set a property of the model that such as "CurrentAction" to "logging-in" Then use that property in the view to NOT draw the validation summary for the register part of the form.
3

Html.ValidationSummary() does not need to be inside of your form element and you only need it the once int most cases. I'd move it outside of your two forms, something like just above your main body content and that should give you the desired effect. I believe in my last app I placed it in the Layout file.

Comments

1

In order to accomplish this you need to separate the two forms, put each one in a partial view, and return the partial view on submit if the validation fails. Change your action result to return a partial result.

the partial views can be rendered in the page using the following:

@Html.partial("_PartialView")

or this way if you need to pass a model

@Html.partial("_Partial", Model)

You can't have two validation summaries on the same page any other way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.