0

The same problem is discussed in this post: Multiple forms in MVC view: ModelState applied to all forms.

I have followed along the post and also have several forms on the same view, custom editor template, have created and registered custom model binder. Here is the accompanying gist.

When I use the Html.ValidationResultFor() helpers in the PasswordEditorViewModel.cshtml editor template I get the desired results:

<li class="error">@Html.ValidationMessageFor(i => i.OldPassword)</li>
<li class="error">@Html.ValidationMessageFor(i => i.NewPassword)</li>
<li class="error">@Html.ValidationMessage("PasswordChangeError.WrongPassword")</li>

This code returns validation errors exactly for the form I've posted.

On the other hand when I use the Html.ValidationSummary() helper in the same editor template the validation result is propagated across all forms despite the fact I've posted only one:

enter image description here

Is this a normal behavior? Or have I missed something in the code?

Community
  • 1
  • 1
lexeme
  • 2,915
  • 10
  • 60
  • 125

1 Answers1

1

When using @Html.ValidationSummary, it does not remember which from was posted, and by which submit button. The way I have handled this in the past is having

<input type="submit" value="button1" />
@if (Request["button1"] != null)
{
    @Html.ValidationSummary()
}

<input type="submit" value="button2" />
@if (Request["button2"] != null)
{
    @Html.ValidationSummary()
}